简体   繁体   English

类型错误:update() 需要 2 个位置参数,但给出了 3 个:Python

[英]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.

相关问题 Python TypeError:接受 4 个位置参数,但给出了 5 个 - Python TypeError: takes 4 positional arguments but 5 were given TypeError: on_voice_state_update() 需要 2 个位置 arguments 但给出了 3 个 - TypeError: on_voice_state_update() takes 2 positional arguments but 3 were given TypeError:update()接受1到2个位置参数,但给出了3个。 - TypeError : update() takes from 1 to 2 positional arguments but 3 were given. python super:TypeError:__init __()接受2个位置参数,但给出了3个 - python super :TypeError: __init__() takes 2 positional arguments but 3 were given Python/MySQL TypeError:execute() 需要 2 到 4 个位置参数,但给出了 5 个 - Python/MySQL TypeError: execute() takes from 2 to 4 positional arguments but 5 were given TypeError: with_column() 从 3 到 4 个位置 arguments 但给出了 5 个(python) - TypeError: with_column() takes from 3 to 4 positional arguments but 5 were given (python) Python TypeError:衍生物_circ()接受2个位置参数,但给出了6个 - Python TypeError: derivatives_circ() takes 2 positional arguments but 6 were given TypeError:__init __()接受2个位置参数,但是给了3个Python 3? - TypeError: __init__() takes 2 positional arguments but 3 were given Python 3? Python - TypeError: generateID() 需要 3 个位置 arguments 但给出了 4 个 - Python - TypeError: generateID() takes 3 positional arguments but 4 were given TypeError:create()接受2个位置参数,但给出了4个 - TypeError: create() takes 2 positional arguments but 4 were given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM