我正在尝试使用以下序列化程序进行POST -
class VariablePUTSerializer(serializers.ModelSerializer):
owner_id = serializers.SerializerMethodField('get_owner_id')
class Meta:
model = Varmst
resource_name = 'varmst'
fields = ('varmst_id', 'varmst_type', 'varmst_name', 'varmst_value', 'varmst_desc',
'varmst_public', 'owner_id', 'varmst_lstchgtm', 'varmst_publish', 'varmst_readonly',
'varmst_calc', 'varmst_starttype', 'varmst_startdt', 'varmst_startcal',
'varmst_offsets', 'varmst_lstval')
def transform_varmst_id(self, obj, value):
maxid = Varmst.objects.latest('varmst_id').varmst_id
if Varmst.objects.filter(varmst_name=obj.varmst_name).exists():
obj.varmst_id = Varmst.objects.filter(varmst_name=obj.varmst_name).values_list('varmst_id')[0]
return obj.varmst_id
else:
obj.varmst_id = maxid + 1
return obj.varmst_id
def get_owner_id(self, obj):
obj.owner_id = Owner.objects.filter(owner_name='Operations').values_list('owner_id')[0][0]
return obj.owner_id
尝试POST数据时会出现以下错误 -
Exception Type: ValueError at /deploy/variable/
Exception Value: Cannot assign "786": "Varmst.owner_id" must be a "Owner" instance.
Varmst.owner_id是对Owner.owner_id的FK引用,我检查了数据库,并且该值(786)确实存在,所以我不确定为什么将它传递给序列化程序会导致它失败。
下面的完整追溯 -
Traceback:
File "D:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Python27\lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "D:\Python27\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
57. return view_func(*args, **kwargs)
File "D:\Python27\lib\site-packages\rest_framework\views.py" in dispatch
400. response = self.handle_exception(exc)
File "D:\Python27\lib\site-packages\rest_framework\views.py" in dispatch
397. response = handler(request, *args, **kwargs)
File "D:\Tidal\API\views.py" in put
343. return HttpResponse(serializer.data, status=status.HTTP_201_CREATED)
File "D:\Python27\lib\site-packages\rest_framework\serializers.py" in data
573. self._data = [self.to_native(item) for item in obj]
File "D:\Python27\lib\site-packages\rest_framework\serializers.py" in to_native
351. value = field.field_to_native(obj, field_name)
File "D:\Python27\lib\site-packages\rest_framework\fields.py" in field_to_native
1035. value = getattr(self.parent, self.method_name)(obj)
File "D:\Tidal\API\serializers.py" in get_owner_id
162. obj.owner_id = Owner.objects.filter(owner_name='Operations').values_list('owner_id')[0][0]
File "D:\Python27\lib\site-packages\django\db\models\fields\related.py" in __set__
339. self.field.name, self.field.rel.to._meta.object_name))
Exception Type: ValueError at /deploy/variable/
Exception Value: Cannot assign "786": "Varmst.owner_id" must be a "Owner" instance.
以下是我的模特 -
class Owner(models.Model):
owner_id = models.IntegerField(primary_key=True, db_column='owner_id')
owner_type = models.SmallIntegerField(blank=True, null=True)
owner_name = models.CharField(max_length=30, blank=True)
owner_allagents = models.CharField(max_length=1, blank=True)
class Meta:
managed = False
db_table = 'owner'
class Varmst(models.Model):
varmst_id = models.IntegerField(primary_key=True, db_column='varmst_id')
varmst_type = models.SmallIntegerField(blank=True, null=True)
varmst_name = models.CharField(max_length=30, blank=True)
varmst_value = models.TextField(blank=True)
varmst_desc = models.TextField(blank=True)
varmst_public = models.CharField(max_length=1, blank=True)
owner_id = models.ForeignKey(Owner, db_column='owner_id')
class Meta:
managed = False
db_table = 'varmst'
答案 0 :(得分:0)
凯文克里斯托弗亨利解决了以下问题 -
如果您为字段所有者命名,那么db_column将自动为owner_id。如果需要,请随意明确db_column,但将该字段命名为owner_id。否则你将失去区分Django模型实例和整数id的能力。