Django REST:嵌套对象add on create(POST)不只是更新(PUT) [英] Django REST: Nested object add on create (POST) not just update (PUT)
问题描述
使用 Django rest
以下是我的serializer.py。
class ProfileSerializer(serializers.ModelSerializer):
pre>
class Meta:
model = Profile
fields =('id','name','active','type')
类型是一个平面视图
然后,被嵌套为这样的每个配置文件...
class TypeSerializer(serializers.ModelSerializer):
class Meta:
model = Type
fields =('id','name','active')
class ProfileSerializer(serializers.ModelSerializer):
type = TypeSerializer()
class Meta:
model = Profile
fields =('id','name','active','type'')
现在这个功能非常完美,但现在我只能在配置文件的详细信息中更新'type',现在是只读的。
如何在创建新配置文件时添加类型,并保持此嵌套查看?
我希望我已经清楚地解释了。
更新:
好的,我只是读这个:
注意:嵌套序列化程序只适用于只读
表示,因为有些情况下它们会有歧义或
非 - 更新实例时使用的显而易见的行为。对于read-write
表示,您应该始终使用一个平面表示,通过使用
一个RelatedField子类。
<所以这是有道理的。所以我改为....type = serializers.PrimaryKeyRelatedField()
这将它放回POST并工作,但这是一个耻辱,我不能用ID和名称来表示类型,所以它使更多感谢最终用户?
解决方案完全支持可写嵌套序列化程序是一个正在进行中,但同时一个解决方案是覆盖
创建
方法在每种情况下的视图:class FooListCreateView(ListCreateAPIView):
model = Foo
serializer_class = FooSerializer
def create(self,request,* args,** kwargs):
data = request.DATA
f = Foo.objects.create()
#...从请求数据创建嵌套对象...
#...
返回响应(serializer.data,
status = status.HTTP_201_CREATED,
headers = headers)
可能不是很理想,对我来说,直到正确的方式来了。
Using Django rest
Below is how I had my serializer.py.
class ProfileSerializer(serializers.ModelSerializer): class Meta: model = Profile fields = ('id', 'name', 'active', 'type')
Type is a flatview
I then changed it so 'type' was nested for each profile like this...
class TypeSerializer(serializers.ModelSerializer): class Meta: model = Type fields = ('id', 'name', 'active') class ProfileSerializer(serializers.ModelSerializer): type = TypeSerializer() class Meta: model = Profile fields = ('id', 'name', 'active', 'type'')
now this works perfect, but I can now only update 'type' when in profile detail it's now read-only.
How can I add type when creating a new profile and still keep this nested view?
I hope I have explained this clearly .
UPDATE:
Ok, I just read this:
Note: Nested serializers are only suitable for read-only representations, as there are cases where they would have ambiguous or non-obvious behavior if used when updating instances. For read-write representations you should always use a flat representation, by using one of the RelatedField subclasses.
So that makes sense. So I changed it to....
type = serializers.PrimaryKeyRelatedField()
That puts it back in the POST and work, but it's a shame, can I not represent 'type' with ID and the name so it makes more sense to the end user?
解决方案Full support of writable nested serializers is a work in progress, but in the mean time one solution is to override the
create
method in the view in each case:class FooListCreateView(ListCreateAPIView): model = Foo serializer_class = FooSerializer def create(self, request, *args, **kwargs): data=request.DATA f = Foo.objects.create() # ... create nested objects from request data ... # ... return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
Probably not ideal, but it works for me until the proper way comes along.
这篇关于Django REST:嵌套对象add on create(POST)不只是更新(PUT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!