2013-02-17 3 views
0

내 옛날 models.py : 남쪽 오류

from django.db import models 
    from taggit.managers import TaggableManager 
    from django.utils.encoding import smart_unicode 
    import markdown 

     class PublishedManager(models.Manager): 
      def get_query_set(self): 
       return super(PublishedManager, self).get_query_set().filter\ 
       (is_published=True) 

     class Category(models.Model): 
      name = models.CharField(max_length=55) 

      def __unicode__(self): 
       return u"%s" %(self.name) 


     class BookMarks(models.Model): 
      name = models.CharField(max_length=255) 
      url = models.URLField() 
      date_time = models.DateTimeField(auto_now_add=True) 


      def __unicode__(self): 
       return u"%s %s %s" %(self.name, self.url, self.date_time) 

     class Post(models.Model): 
      title = models.CharField(max_length=255) 
      slug = models.SlugField(max_length=255, unique=True) 
      excerpt = models.TextField(blank=True, help_text="A small teaser of\ 
         your content") 
      content = models.TextField(blank=True, null=True) 
      contentmarkdown = models.TextField() 
      date_created = models.DateTimeField(auto_now_add=True) 
      is_published = models.BooleanField(default=True) 
      objects = models.Manager() 
      published_objects = PublishedManager() 
      tags = TaggableManager() 
      category = models.ForeignKey(Category) 

      def save(self): 
       self.content = markdown.markdown(self.contentmarkdown) 
       super(Post, self).save() # Call the "real" save() method. 

      class Meta: 
       ordering = ("date_created",) 

      def __unicode__(self): 
       return smart_unicode("%s %s %s %s %s" %(self.title, self.content, self.is_published, self.category, self.tags)) 

      def get_absolute_url(self): 
       return "/posts/%s/" % self.id 



     class Book(models.Model): 
      name = models.CharField(max_length=55) 
      author = models.CharField(max_length=55) 
      image = models.ImageField(upload_to="static/img/books/") 
      body = models.TextField(blank=True, null=True) 
      bodymarkdown = models.TextField() 


      def __unicode__(self): 
       return u"%s %s" %(self.name, self.author) 

      def get_absolute_url(self): 
       return "/books/%s/" % self 

.id 

및 추가 한 후

"카테고리 = models.ForeignKey (카테고리)"이 책의 필드와 북마크 테이블이 같은 나의 새로운 models.py :

from django.db import models 
from taggit.managers import TaggableManager 
from django.utils.encoding import smart_unicode 
import markdown 

class PublishedManager(models.Manager): 
    def get_query_set(self): 
     return super(PublishedManager, self).get_query_set().filter\ 
     (is_published=True) 

class Category(models.Model): 
    name = models.CharField(max_length=55) 

    def __unicode__(self): 
     return u"%s" %(self.name) 


class BookMarks(models.Model): 
    name = models.CharField(max_length=255) 
    url = models.URLField() 
    date_time = models.DateTimeField(auto_now_add=True) 
    category = models.ForeignKey(Category) 

    def __unicode__(self): 
     return u"%s %s %s" %(self.name, self.url, self.date_time) 

class Post(models.Model): 
    title = models.CharField(max_length=255) 
    slug = models.SlugField(max_length=255, unique=True) 
    excerpt = models.TextField(blank=True, help_text="A small teaser of\ 
       your content") 
    content = models.TextField(blank=True, null=True) 
    contentmarkdown = models.TextField() 
    date_created = models.DateTimeField(auto_now_add=True) 
    is_published = models.BooleanField(default=True) 
    objects = models.Manager() 
    published_objects = PublishedManager() 
    tags = TaggableManager() 
    category = models.ForeignKey(Category) 

    def save(self): 
     self.content = markdown.markdown(self.contentmarkdown) 
     super(Post, self).save() # Call the "real" save() method. 

    class Meta: 
     ordering = ("date_created",) 

    def __unicode__(self): 
     return smart_unicode("%s %s %s %s %s" %(self.title, self.content, self.is_published, self.category, self.tags)) 

    def get_absolute_url(self): 
     return "/posts/%s/" % self.id 



class Book(models.Model): 
    name = models.CharField(max_length=55) 
    author = models.CharField(max_length=55) 
    image = models.ImageField(upload_to="static/img/books/") 
    body = models.TextField(blank=True, null=True) 
    bodymarkdown = models.TextField() 
    category = models.ForeignKey(Category) 

    def __unicode__(self): 
     return u"%s %s" %(self.name, self.author) 

    def get_absolute_url(self): 
     return "/books/%s/" % self.id 

class Errors(models.Model): 
    name = models.CharField(max_length=255) 
    created = models.DateTimeField(auto_now_add=True) 
    body = models.TextField(blank=True, null=True) 
    bodymarkdown = models.TextField() 

    def __unicode__(self): 
     return self.name 

    def get_absolute_url(self): 
     return "/errors/%s/" % self.id 

나는 남쪽을 사용했다. 모든 것이 OK 가고 있었다 그러나이 줄 끝 :

? 1. Quit now, and add a default to the field in models.py 
? 2. Specify a one-off value to use for existing columns now 
? 3. Disable the backwards migration by raising an exception. 
? Please select a choice: 2 
? Please enter Python code for your one-off default value. 
? The datetime module is available, so you can do e.g. datetime.date.today() 
>>> datetime.datetime.now() 

나는 다음과 같은 오류가 발생합니다 :

Traceback (most recent call last): 
    File "manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line 
    utility.execute() 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv 
    self.execute(*args, **options.__dict__) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute 
    output = self.handle(*args, **options) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/management/commands/migrate.py", line 108, in handle 
    ignore_ghosts = ignore_ghosts, 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/__init__.py", line 213, in migrate_app 
    success = migrator.migrate_many(target, workplan, database) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 235, in migrate_many 
    result = migrator.__class__.migrate_many(migrator, target, migrations, database) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 310, in migrate_many 
    result = self.migrate(migration, database) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 133, in migrate 
    result = self.run(migration) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 106, in run 
    dry_run.run_migration(migration) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 191, in run_migration 
    self._run_migration(migration) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 178, in _run_migration 
    raise exceptions.FailedDryRun(migration, sys.exc_info()) 
south.exceptions.FailedDryRun: ! Error found during dry run of '0012_auto__del_field_book_category__add_field_book_category1__del_field_boo'! Aborting. 
Traceback (most recent call last): 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 175, in _run_migration 
    migration_function() 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 57, in <lambda> 
    return (lambda: direction(orm)) 
    File "/home/ada/mainproject/blog/migrations/0012_auto__del_field_book_category__add_field_book_category1__del_field_boo.py", line 17, in forwards 
    keep_default=False) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/db/generic.py", line 44, in _cache_clear 
    return func(self, table, *args, **opts) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/db/generic.py", line 402, in add_column 
    sql = self.column_sql(table_name, name, field) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/db/generic.py", line 688, in column_sql 
    default = field.get_db_prep_save(default, connection=self._get_connection()) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 991, in get_db_prep_save 
    connection=connection) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 292, in get_db_prep_save 
    prepared=False) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 284, in get_db_prep_value 
    value = self.get_prep_value(value) 
    File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 537, in get_prep_value 
    return int(value) 
TypeError: int() argument must be a string or a number, not 'datetime.datetime' 

나는이 오류가 왜 당신이 말해 줄 수주십시오? 나는 무엇을해야만 하는가?

답변

1

새로운 카테고리 필드는 필수 항목이므로 (카테고리를 지정하지 않고 책이나 북마크를 만들 수 없음) 남쪽에서 데이터베이스의 기존 책과 북마크를 어떻게 처리할지 묻는 메시지가 표시됩니다. 현재 모든 책/북마크는 유효하지 않습니다 (카테고리가 없기 때문에).

새 카테고리 필드는 카테고리 테이블의 기본 키를 사용하여 북/북 테이블에 표시됩니다. 이 기본 키는 정수 (또는 가능한 문자열) 일 수 있습니다.

남쪽에서 기본 기본 키를 입력하라는 메시지가 표시됩니다. 데이터베이스에 범주 개체의 기본 키 (정수 또는 문자열)를 제공하는 대신. 당신은 datatime 객체를 제공했습니다.

필드에 datetime 개체가 없기 때문에 마이그레이션을 실제로 실행하면 문제가 발생합니다. 정수 (또는 문자열)를 포함합니다. 이주를 다시 작성하고 올 Y 른 기본 키를 추가해야합니다.

+0

대단히 감사합니다. 멋진 설명입니다. –

관련 문제