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 は、デフォルトの主キーを提供するように依頼しました。データベース内のカテゴリ オブジェクトの主キー (整数または文字列) を指定する代わりに。datatime オブジェクトを指定しました。

フィールドが日時オブジェクトを保持していないため、実際に移行を実行するとうまくいきません。整数 (または文字列) を保持します。移行を再度作成し、有効な主キーを追加する必要があります。

于 2013-02-17T21:17:30.983 に答える