0
當提示PUT(更新)出這個錯誤如何使用方法PUT Django的REST的架構更新
不能實例序列化實例時調用.is_valid()因爲沒有數據=關鍵字參數傳遞。 我的觀點:
def get_serializer(self, *args, **kwargs):
queryset = Producer.objects.get(pk=self.kwargs['pk'])
if self.request.user.is_authenticated:
return ProducerSerializer(queryset,
fields=('short_info',))
if self.request.method == 'PUT' or self.request.method == 'PATCH':
return ProducerUpdateSerializer
else:
return ProducerSerializer(queryset,
fields=('website', 'phone', 'email', 'contacts', 'short_info'))
def get_queryset(self):
return Producer.objects.filter(pk=self.kwargs['pk'])
我的串行
class DynamicFieldsModelSerializer(serializers.ModelSerializer):
def __init__(self, *args, **kwargs):
# Don't pass the 'fields' arg up to the superclass
fields = kwargs.pop('fields', None)
# Instantiate the superclass normally
super(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs)
if fields is not None:
for i in fields:
print self.fields.pop(i)
class ProducerSerializer(DynamicFieldsModelSerializer):
tags = TagListSerializerField()
categories = serializers.StringRelatedField(many=True)
contacts = ProducerContactSerializer(many=True)
country = CountryField(country_dict=True)
business_type = ChoicesSerializerField()
class Meta:
model = Producer
fields = ('id', 'business_type', 'logo', 'name', 'slug', 'country', 'city',
'street_address', 'zip', 'short_info', 'info', 'website',
'categories', 'tags', 'contacts', 'email', 'phone')
class ProducerUpdateSerializer(serializers.ModelSerializer):
tags = TagListSerializerField()
categories = serializers.StringRelatedField(many=True)
class Meta:
model = Producer
fields = ('id', 'business_type', 'logo', 'name', 'slug', 'country', 'city',
'street_address', 'zip', 'short_info', 'info', 'website', 'categories', 'tags')
實例時提示爲無數據=關鍵字參數傳遞PUT(更新)出這個錯誤
不能調用.is_valid()的時候串行器實例。