我有数据我正试图进入数据库,它包含一个我想用来为另一个字段生成数据的字段。我完全混淆了如何让我的序列化程序忽略但仍然使用obj.owner_name来获取owner_id用于PUT。
如果有人能指出我做错了什么就会很棒。
<root>
<list-item>
<varmst_id>1867</varmst_id>
<varmst_type>3</varmst_type>
<varmst_name>var_date </varmst_name>
<varmst_value>20140911</varmst_value>
<varmst_desc/>
<varmst_public>Y</varmst_public>
<owner_name>Operations</owner_name>
</list-item>
</root>
这是我的序列化程序 -
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')
exclude_fields = ('owner_name',)
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=obj.owner_name).values_list('owner_id')[0]
return obj.owner_id
当我尝试使用上面的数据进行PUT时,这是我的追溯 -
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 Response(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
163. obj.owner_id = Owner.objects.filter(owner_name=obj.owner_name).values_list('owner_id')[0]
Exception Type: AttributeError at /deploy/variable/
Exception Value: 'Varmst' object has no attribute 'owner_name'
我知道'Varmst'没有名为'owner_name'的表。仅仅因为序列化器具有它并不意味着我将使用它....
答案 0 :(得分:2)
你可以让你的方法transform_varmst_id
更加pythonic:
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]
else:
obj.varmst_id = maxid + 1
return obj.varmst_id
在这种特殊情况下,不需要多个return
语句。
编辑:
每次调用方法时都不需要创建变量maxid
。把它放在条件中:
def transform_varmst_id(self, obj, value):
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]
else:
maxid = Varmst.objects.latest('varmst_id').varmst_id
obj.varmst_id = maxid + 1
return obj.varmst_id
这更像是pythonic。您当然可以将else
块减少为:
obj.varmst_id = Varmst.objects.latest('varmst_id').varmst_id + 1
但这是品味问题。我更喜欢较短的陈述,纵向优于横向。