gpt4 book ai didi

Django 模型 - 无法序列化为迁移文件

转载 作者:行者123 更新时间:2023-12-04 00:07:22 25 4
gpt4 key购买 nike

我有 2 款车型 Car 和 Offer。汽车表是一个填充了车辆规范的现有表。在 Django 管理员中,我想将报价映射到规范表中的现有汽车。这意味着当单击管理中的报价时,我希望能够看到包含所有汽车的列表 - 找到正确的汽车 - 并将其保存在报价中。我已经通过使用基于现有汽车对象的选择列表填充外键字段来完成此操作。

模型.py:

class Car(models.Model):
brand = models.TextField(max_length=300, default= "")
model = models.TextField(max_length=300, default= "")
edition = models.TextField(max_length=300, default= "")
engineVolume = models.FloatField(default=0.0)

def __unicode__(self):
return smart_unicode(self.brand)

carIds = []
idx = 1
for car in Car.objects.all():
carIds.append((idx, car))
idx = idx + 1


class Offer(models.Model):
stringUrl = models.TextField(max_length=300)
extractionDate = models.DateTimeField(default=datetime.datetime.now, blank=True)
cars = models.ForeignKey(Car, default= "", choices=carIds, null=True, to_field='id')

这很好用。我点击管理中的一个报价,我看到一个选择框填充了所有现有的汽车。我找到了正确的汽车,保存它,数据库中报价的外键汽车 ID 指向正确的车辆。
但是突然当我想进行以后的迁移时,django 说它无法序列化汽车对象?
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site- packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 143, in handle
self.write_migration_files(changes)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 171, in write_migration_files
migration_string = writer.as_string()
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 166, in as_string
operation_string, operation_imports = OperationWriter(operation).serialize()
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 124, in serialize
_write(arg_name, arg_value)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 87, in _write
arg_string, arg_imports = MigrationWriter.serialize(_arg_value)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 377, in serialize
return cls.serialize_deconstructed(path, args, kwargs)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 268, in serialize_deconstructed
arg_string, arg_imports = cls.serialize(arg)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 303, in serialize
item_string, item_imports = cls.serialize(item)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 303, in serialize
item_string, item_imports = cls.serialize(item)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 465, in serialize
"topics/migrations/#migration-serializing" % (value, get_docs_version())
`enter code here`ValueError: Cannot serialize: <Car: Nissan>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/1.8/topics/migrations/#migration- serializing

我不知道为什么会出现这个错误。我是 Django 的新手,刚开始接触管理员。我一直在阅读有关序列化和解构的内容,但看不到如何在此处应用它?也许我应该走不同的路线来实现我想要的?

最佳答案

在我的情况下,我试图添加一个字段,如:

task = models.ForeignKey(Task, on_delete=models.CASCADE, default=Task.objects.first())
但通过使用纠正了这一点:
task = models.ForeignKey(Task, on_delete=models.CASCADE, default=Task.objects.first().pk)

关于Django 模型 - 无法序列化为迁移文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32760038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com