我试图通过在类的扩展中使用ModelSerializer的create方法来保存一些工作,然后在扩展中添加我需要的额外字段。当我这样做时,我从DRF收到一个错误,指出不支持嵌套序列化程序中的可写字段。有没有办法实现这一点,以便我不必显式定义create方法中的每个字段,而是将该工作推送到超级构造函数?包括我的代码:
class CreateUserSerializer(ModelSerializer):
school = SchoolSerializer(required=False)
class Meta:
model = User
fields = ('id', 'username', 'password', 'first_name', 'last_name',
'user_type', 'school', 'email')
extra_kwargs = {
'password': {'write_only': True},
'user_type': {'read_only': True}
}
def create(self, validated_data):
original_validated_data = validated_data.copy()
if 'password' in validated_data:
password = validated_data.pop('password')
user = super(CreateUserSerializer, self).create(validated_data)
if 'password' in original_validated_data:
user.set_password(original_validated_data['password'])
if 'school' in original_validated_data:
user.user_type = User.TYPE_ADVISOR
return user
这是我得到的错误:
File "/serializers/user.py", line 41, in create
user = super(CreateUserSerializer, self).create(validated_data)
File "/lib/python2.7/site-packages/rest_framework/serializers.py", line 832, in create
raise_errors_on_nested_writes('create', self, validated_data)
File "/lib/python2.7/site-packages/rest_framework/serializers.py", line 724, in raise_errors_on_nested_writes
class_name=serializer.__class__.__name__
AssertionError: The `.create()` method does not support writable nestedfields by default.
Write an explicit `.create()` method for serializer `api.serializers.user.CreateUserSerializer`, or set `read_only=True` on nested serializer fields.
我正在使用Rest Framework V3.3.1
答案 0 :(得分:6)
我认为这不会起作用,因为有效地调用超级意味着你正在调用根据文档不支持编写嵌套模型的基本方法。
要解决此问题,请查看:
http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers
答案 1 :(得分:3)
create()
方法不支持可写的嵌套字段。您正试图通过调用school
在create()
中保存super
对象。
相反,您可以尝试:
def create(self, validated_data):
original_validated_data = validated_data.copy()
if 'school' in validated_data:
school = validated_data.pop('school')
schoolObj = School.objects.create(**school) #Assuming your 'school' model name is School
if 'password' in validated_data:
password = validated_data.pop('password')
user = User.objects.create(school=schoolObj,**validated_data)
if 'password' in original_validated_data:
user.set_password(original_validated_data['password'])
if 'school' in original_validated_data:
user.user_type = User.TYPE_ADVISOR
user.save()
return user