TypeError与模型字段

时间:2015-01-04 10:44:01

标签: python django sqlite

我在模型中添加了一些新字段,django建议我添加默认值或在模型中设置一些默认参数。我将datetime对象添加为值,但值应为int或number。现在我无法同步db因为类型错误。我试图删除一些对象,这些对象是这个模型的实例,我在那里做了一些修改,但它对我没有帮助。

我现在如何修复我的模型?!

这是追溯

Applying card.0026_auto_20150104_1323...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\core\management\__init__.p
y", line 385, in execute_from_command_line
    utility.execute()
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\core\management\__init__.p
y", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\core\management\base.py",
line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\core\management\base.py",
line 338, in execute
    output = self.handle(*args, **options)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\core\management\base.py",
line 533, in handle
    return self.handle_noargs(**options)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\core\management\commands\s
yncdb.py", line 27, in handle_noargs
    call_command("migrate", **options)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\core\management\__init__.p
y", line 115, in call_command
    return klass.execute(*args, **defaults)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\core\management\base.py",
line 338, in execute
    output = self.handle(*args, **options)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\core\management\commands\m
igrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\db\migrations\executor.py"
, line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\db\migrations\executor.py"
, line 97, in apply_migration
    migration.apply(project_state, schema_editor)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\db\migrations\migration.py
", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, ne
w_state)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\db\migrations\operations\f
ields.py", line 37, in database_forwards
    field,
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\db\backends\sqlite3\schema
.py", line 167, in add_field
    self._remake_table(model, create_fields=[field])
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\db\backends\sqlite3\schema
.py", line 74, in _remake_table
    self.effective_default(field)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\db\backends\schema.py", li
ne 184, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\db\models\fields\related.p
y", line 1735, in get_db_prep_save
    return self.related_field.get_db_prep_save(value, connection=connection)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\db\models\fields\__init__.
py", line 627, in get_db_prep_save
    prepared=False)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\db\models\fields\__init__.
py", line 907, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\RIK\Envs\la\lib\site-packages\django\db\models\fields\__init__.
py", line 915, in get_prep_value
    return int(value)
TypeError: int() argument must be a string or a number, not 'datetime.datetime'

这是模型代码的一部分:

我在CollectionItem对象中添加了owner字段:

class CollectionItem (models.Model):
    card = models.ForeignKey(Card)
    golden = models.BooleanField(default=False)
    count = models.IntegerField(default=0)
    level = models.IntegerField(default=1)
    owner = models.ForeignKey(User, related_name='collectionCards')

    @property
    def price(self):
        return self.card.price



class Collection (models.Model):
    owner = models.ForeignKey(User, related_name='collections')
    items = models.ManyToManyField(CollectionItem, through='CollectionCollector')


class CollectionAdmin (admin.ModelAdmin):
    fields = ['owner'] admin.site.register(Collection, CollectionAdmin)

class CollectionCollector (models.Model):
    item = models.ForeignKey(CollectionItem, related_name='collection_item')
    collection = models.ForeignKey(Collection)

3 个答案:

答案 0 :(得分:0)

我认为,您在迁移过程中将datetime.datetime.now()作为默认值,因为它要求您输入。

只需编辑迁移文件,并将日期时间默认值更改为其中一个字段的字符串

count = models.IntegerField(default=0)
level = models.IntegerField(default=1)

然后再次运行

答案 1 :(得分:0)

我在我的环境中面对同样的情况,以下是我采取的解决方案: 删除 db.sqlite3 删除所有应用中的所有迁移文件 然后重新运行评论:

python manage.py makemigrations
python manage.py migrate

完成这些步骤后,不会再出现任何错误。 我还在https://stackoverflow.com/a/34825277/6711680

中找到了类似的解决方案链接

答案 2 :(得分:0)

删除您应用目录下迁移文件夹中的旧python文件,然后再次进行迁移。它将解决问题。