【发布时间】:2019-01-15 21:09:09
【问题描述】:
所以我有一个带有一个外键引用的模型,它在几周内运行良好。我最终不得不向模型添加另一个外键引用,现在我无法使用序列化程序保存任何记录。我应该提到我正在使用 Django 2.1。 这是我正在使用的模型(为简单起见,删除了一些字段):
class Finding(models.Model):
finding_id = models.IntegerField(null=False, blank=False, primary_key=True, db_index=True)
name = models.CharField(null=True, blank=True, max_length=255)
web_app = models.ForeignKey(WebApplication, on_delete=models.CASCADE)
q_id = models.ForeignKey(StagingQkb, on_delete=models.CASCADE)
新的 FK 是 q_id 字段。这只是一个简单的 int 之前。我实际上已经炸毁了旧表,而新表仍然完全是空的,所以我知道现有数据没有问题(带有外键的表仍然完好无损)。之前保存结果时,我只会为“web_app”提供 WebApplication 对象的 PK。据我所知,这仍然有效。 'q_id' 字段,当以相同的方式插入时,抱怨它需要一个 int/string/bytes 而不是 StagingQkb 对象。好吧,我没有给它一个 StagingQkb 对象,所以给它什么!
这是序列化程序:
class FindingSerializer(serializers.ModelSerializer):
class Meta:
model = Finding
fields = '__all__'
我提供给序列化程序的数据如下所示:
data = {'finding_id': 5514989,
'name': 'Sample-Name',
'q_id': 12345,
'web_app': 67890}
当我将数据插入序列化程序时,我正在执行以下操作:
>>> fs = FindingSerializer(data=data)
>>> fs.is_valid()
True
>>> fs.save()
运行上述代码后出现的错误如下所示:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/opt/oss/python35/lib/python3.5/site-packages/rest_framework/serializers.py", line 214, in save
self.instance = self.create(validated_data)
File "/opt/oss/python35/lib/python3.5/site-packages/rest_framework/serializers.py", line 959, in create
raise TypeError(msg)
TypeError: Got a `TypeError` when calling `Finding.objects.create()`. This may be because you have a writable field on the serializer class that is not a valid argument to `Finding.objects.create()`. You may need to make the field read-only, or override the FindingSerializer.create() method to handle this correctly.
Original exception was:
Traceback (most recent call last):
File "/opt/oss/python35/lib/python3.5/site-packages/rest_framework/serializers.py", line 940, in create
instance = ModelClass._default_manager.create(**validated_data)
File "/opt/oss/python35/lib/python3.5/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/opt/oss/python35/lib/python3.5/site-packages/django/db/models/query.py", line 413, in create
obj.save(force_insert=True, using=self.db)
File "/home/dpz3w0q/AutonomousPrimeD/autonomousprimed/Finding/models.py", line 83, in save
q_id=self.q_id)
File "/home/dpz3w0q/AutonomousPrimeD/autonomousprimed/Finding/models.py", line 173, in evaluate
if ScoreMatrix.objects.filter(q_id=q_id).count() > 0:
File "/opt/oss/python35/lib/python3.5/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/opt/oss/python35/lib/python3.5/site-packages/django/db/models/query.py", line 844, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/opt/oss/python35/lib/python3.5/site-packages/django/db/models/query.py", line 862, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/opt/oss/python35/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1263, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/opt/oss/python35/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1287, in _add_q
split_subq=split_subq,
File "/opt/oss/python35/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1225, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/opt/oss/python35/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1096, in build_lookup
lookup = lookup_class(lhs, rhs)
File "/opt/oss/python35/lib/python3.5/site-packages/django/db/models/lookups.py", line 20, in __init__
self.rhs = self.get_prep_lookup()
File "/opt/oss/python35/lib/python3.5/site-packages/django/db/models/lookups.py", line 70, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "/opt/oss/python35/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1807, in get_prep_value
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'StagingQkb'
我完全被困在这里了。我没有改变将数据传递给序列化程序的方式,所以我真的不知道我做错了什么。如果有人能够帮助我解决这个问题,我将不胜感激!
【问题讨论】:
-
回溯似乎表明在
models.py中运行了一些额外的代码——可能从Finding.save()调用。你能显示来自models.py的所有代码吗? -
您好,谢谢,这是一个很好的观点!保存方法如下所示: def save(self, force_insert=False, force_update=False, using=None, update_fields=None): if self.web_app.sys_id is not None and self.web_app.sys_id.composite_dav is not None and \ self.q_type 不是 'INFORMATION_GATHERED': self.rating = ScoreMatrix.evaluate(dav=self.web_app.sys_id.composite_dav, severity=self.severity, q_id=self.q_id) else: self.rating = None super() .save()
-
我将尝试更改对 self.q_id 的引用以引用外部表的 PK,并在我知道它是否有效后报告。
标签: python django django-models django-rest-framework django-serializer