简体   繁体   中英

TypeError: update() takes 2 positional arguments but 3 were given : Python

I'm getting 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 :


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 . 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() .

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:

class ShopOwnerOrderManageSerializer():
    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):
        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 

Since however Django's ModelSerializer will implement the update like we did here, you can omit it completely:

class ShopOwnerOrderManageSerializer():
    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM