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

在将“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'

请你能告诉我为什么我会收到这个错误吗?我必须做什么?

4

1 回答 1

1

因为您的新类别字段是强制性的(如果不给它一个类别就无法创建书籍或书签),South 询问您如何处理数据库中现有的书籍和书签 - 您的最新更改会生成所有当前书籍/ 书签无效(因为它们没有类别)。

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

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

它在实际运行迁移时会出错,因为该字段不包含日期时间对象。它保存整数(或字符串)。您需要再次创建迁移并添加有效的主键。

于 2013-02-17T21:17:30.983 回答