django南方错误

时间:2013-02-17 21:05:18

标签: django django-south

我的旧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

并在将“category = models.ForeignKey(Category)”这个字段添加到Book和BookMarks表之后,我的新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

我用过南方。一切都顺利但是在这一行之后:

?  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 个答案:

答案 0 :(得分:1)

因为您的新类别字段是必填字段(您无法在不为其提供类别的情况下创建图书或书签),南方会询问您如何处理数据库中的现有图书和书签 - 您的最新更改将使您当前的图书/书签无效(因为他们没有类别)。

您的新类别字段使用类别表的主键在书籍/书签表中显示。该主键可能是整数(或可能是字符串)。

South要求您提供默认主键。而不是在数据库中提供类别对象的主键(可以是整数或字符串)。您已经提供了数据时对象。

当它实际运行迁移时它正在崩溃,因为该字段不包含datetime对象。它包含整数(或字符串)。您需要再次创建迁移并添加有效的主键。