[英]TypeError: update() takes 2 positional arguments but 3 were given : Python
I'm getting TypeError
: update() takes 2 positional arguments but 3 were given
.我收到TypeError
: update() takes 2 positional arguments but 3 were given
。 I have no idea about why this error occurs.我不知道为什么会发生此错误。 if anybody could figure out where i'm doing thing wrong then would be much appreciated.如果有人能弄清楚我在哪里做错了,那么将不胜感激。 thank you so much in advance.非常感谢你。
serializers.py :序列化程序.py:
class ShopOwnerOrderManageSerializer(Serializer):
invoice_no = CharField(read_only=True)
shopowner_expected_date = CharField(allow_blank=True)
shopowner_estimated_time = CharField(allow_blank=True)
status = CharField(allow_blank=True)
class Meta:
model = CarOwnerShopConfirm
fields= ['shopowner_expected_date','shopowner_estimated_time','status']
def update(self,validated_data):
shopowner_expected_date = validated_data['shopowner_expected_date']
shopowner_estimated_time = validated_data['shopowner_estimated_time']
status = validated_data['status']
shopconfirm_obj = CarOwnerShopConfirm.objects.update(
shopowner_expected_date = shopowner_expected_date,
shopowner_estimated_time = shopowner_estimated_time,
status = status
)
shopconfirm_obj.save()
return validated_data
update
in a Serializer
takes three parameters: self
, * instance*
and validated data . Serializer
update
采用三个参数: self
、 * instance*
和验证数据。 Normally the database does not save the changes in the database, but only updates the instance.通常情况下,数据库不保存在数据库中的变化,但只更新实例。 The view for example can then later decide to update the database by calling .save()
.例如,视图随后可以决定通过调用.save()
来更新数据库。
You probably also want to use a ModelSerializer
, since a simple Serializer
does not take into account the Meta
inner class, and thus will not "bind" the serializer fields to these of the object:您可能还想使用ModelSerializer
,因为简单的Serializer
不考虑Meta
内部类,因此不会将序列化器字段“绑定”到对象的这些字段:
class ShopOwnerOrderManageSerializer(ModelSerializer):
invoice_no = CharField(read_only=True)
shopowner_expected_date = CharField(allow_blank=True)
shopowner_estimated_time = CharField(allow_blank=True)
status = CharField(allow_blank=True)
class Meta:
model = CarOwnerShopConfirm
fields= ['shopowner_expected_date','shopowner_estimated_time','status']
def update(self, instance, validated_data):
instance.shopowner_expected_date = validated_data.get('shopowner_expected_date', instance.shopowner_expected_date)
instance.shopowner_estimated_time = validated_data.get('shopowner_estimated_time', intance.shopowner_estimated_time)
instance.status = validated_data.get('status', instance.status)
return instance
Since however Django's ModelSerializer
will implement the update
like we did here, you can omit it completely:然而,由于 Django 的ModelSerializer
会像我们在这里所做的那样实现update
,你可以完全省略它:
class ShopOwnerOrderManageSerializer(ModelSerializer):
invoice_no = CharField(read_only=True)
shopowner_expected_date = CharField(allow_blank=True)
shopowner_estimated_time = CharField(allow_blank=True)
status = CharField(allow_blank=True)
class Meta:
model = CarOwnerShopConfirm
fields= ['shopowner_expected_date','shopowner_estimated_time','status']
# no update
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.