92 搴忓垪鍖? 鍙嶅簭鍒楀寲

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了92 搴忓垪鍖? 鍙嶅簭鍒楀寲相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%a0%a1%e9%aa%8c' title='鏍¢獙'>鏍¢獙   put   eth   add   lan   灏辨槸   self   _id   post   

涓昏鍐呭:

1 鍙嶅簭鍒楀寲(post璇锋眰)

銆€銆€a : 鎻愪氦post璇锋眰, 鍏堢‘瀹氭柊澧炵殑鏁版嵁缁撴瀯,

銆€銆€b: 瑙e喅搴忓垪鍖栧拰鍙嶅簭鍒楀寲鐨勫瓧娈典笉缁熶竴鐨勬儏妗?搴忓垪鍖栧櫒涓湁鐨勫瓧娈? 瑕佸簭鍒楀寲鐨勫瓧娈靛繀椤绘湁.)

銆€銆€銆€銆€required = False, 鍙簭鍒楀寲, 涓嶈蛋鏍¢獙

銆€銆€銆€銆€read_only:           鍙蛋搴忓垪鍖?/p>

銆€銆€銆€銆€write_only:           鍙蛋鍙嶅簭鍒楀寲

銆€銆€c: 瀵逛簬鍓嶇浼犺繃鏉ョ殑鏁版嵁杩涜鏍¢獙. 瑕佽Е鍙戝弽搴忓垪鍖栬繃绋嬩腑鐨刢reate鏂规硶,

 def create(self, validated_data):
        # 浣跨敤orm鎿嶄綔鍒涘缓瀵硅薄
        book_obj = models.Book.objects.create(title=validated_data[鈥榯itle鈥榏,pub_time=validated_data[鈥榩ub_time鈥榏, category=validated_data[鈥榩ost_category鈥榏, publisher_id=validated_data[鈥榩ublisher_id鈥榏)
        book_obj.authors.add(*validated_data[鈥榓uthors_list鈥榏)
        return book_obj

銆€銆€d: 閫氳繃楠岃瘉杩斿洖ser_obj.validated_data, 涓嶉€氳繃楠岃瘉杩斿洖ser_obj.errors

2 鍙嶅簭鍒楀寲put璇锋眰

銆€銆€a :  鎻愪氦put璇锋眰, 鏇存柊鏁版嵁

銆€銆€b :  鍙嶅簭鍒楀寲鏃? 瑕佹妸鍙嶅簭鍒楀寲鐨勫璞′紶缁欏搴旂殑instance瀹炰緥,  data = request.data, partial=true閮ㄥ垎鏇存柊.

銆€銆€c : 鍙嶅簭鍒楀寲璺熸柊鏃惰Е鍙憉pdate鏂规硶: 

    def update(self, instance, validated_data):
        # 浣跨敤orm鎿嶄綔缂栬緫瀵硅薄
        print(instance)             # 鏇存柊鐨刡ook_obj瀵硅薄
        print(validated_data)       # 鏍¢獙閫氳繃鐨勬暟鎹?        instance.title = validated_data.get(鈥榯itle鈥? instance.title)
        instance.pub_time = validated_data.get(鈥榩ub_time鈥? instance.pub_time)
        instance.category = validated_data.get(鈥榩ost_category鈥? instance.category)
        instance.publisher_id = validated_data.get(鈥榩ublisher_id鈥? instance.publisher_id)
        if validated_data.get(鈥榓uthors_list鈥?:
            instance.authors.set(validated_data[鈥榓uthors_list鈥榏)
        instance.save()
        return instance

銆€銆€d : 楠岃瘉閫氳繃杩斿洖ser_obj.validated_data, 涓嶉€氳繃杩斿洖ser_obj.errors

銆€銆€e : 鍗曚釜瀛楁鐨勬牎楠?(鏉冮噸涓棿)

    def validate_title(self, value):
    # value灏辨槸title鐨勫€?瀵箆alue澶勭悊
        if "python" not in value.lower():
            raise serializers.ValidationError("鏍囬蹇呴』鍚湁python")
        return value

銆€銆€銆€銆€澶氫釜瀛楁鐨勬牎楠?(鏉冮噸鏈€浣?

    # # 妫€楠屽涓瓧娈?    # def validate(self, attrs):銆€銆€銆€銆€attrs鏄竴涓瓧鍏?    #     if 鈥榖ad language鈥?not in attrs[鈥榯itle鈥榏 and attrs[鈥榩ost_category鈥榏 == 1:
    #         return attrs
    #     else:
    #         raise serializers.ValidationError(鈥樻湁鏁忔劅淇℃伅鈥?

銆€銆€銆€銆€鑷畾涔夊瓧娈垫牎楠?鏉冮噸鏈€楂?/span>)

def my_validate(value):
    print(鈥榯his  is validate鈥?value)
    if 鈥樹附涓解€榠n value.lower():
        raise serializers.ValidationError(鈥樹笉涓ヨ皑鈥?
    return value

銆€銆€銆€銆€銆€銆€娉ㄦ剰:缁欏瓧娈甸厤缃畍alidators = [my_validate]

class BookSerializer(serializers.Serializer):
#     # 搴忓垪鍖栧櫒涓湁鐨勫瓧娈靛璞′腑蹇呴』鏈?#     title = serializers.CharField(max_length=32, validators=[my_validate, ])

3 ModelSerializer: 鐢变簬鑷畾涔夊簭鍒楀寲鍣ㄧ被姣旇緝楹荤儲, 杩樿鍐檆reate,update鏂规硶, 鎵€浠ュ紩鍏ヤ簡ModelSerializer.

銆€銆€銆€銆€a : 瀹氫箟涓€涓被缁ф壙serializers.ModelSerializer : 浼氳浣犺繖浜涙墍鏈夌殑澶栭敭鍏崇郴鍙樻垚read_only = True

銆€銆€銆€銆€b : 鍦ㄧ被閲岄潰瀹氫箟涓€涓猰eta绫?

銆€銆€銆€銆€銆€銆€绫婚噷闈㈢殑瀛楁model绛変簬琛ㄥ悕,

銆€銆€銆€銆€銆€銆€fieds绛変簬鎵€鏈夌殑瀛楁: fields = "__all__"

銆€銆€銆€銆€銆€銆€depth: 琛ㄧず灞傜骇鍏崇郴, 鏈€濂戒笉瑕佽秴杩囦笁绾?

銆€銆€銆€銆€銆€銆€extra_kwargs = {鈥樺瓧娈靛悕绉扳€?鈥樿嚜瀹氫箟鐨勫弬鏁伴厤缃俊鎭€?}

銆€銆€銆€銆€c : 鑷畾涔夋柟娉曞瓧娈?  SerializerMethodField()  鏂规硶瀛楁

銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€def get_瀛楁鍚嶇О(self, obj):

銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€obj  姣忔搴忓垪鍖栫殑妯″瀷瀵硅薄

銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€return  鑷畾涔夌殑鏁版嵁

銆€銆€銆€銆€d : 瀹炰緥:

銆€銆€銆€銆€銆€銆€鑷畾涔夋牎楠屾柟娉曞瓧娈? 鏄剧ず鑷繁闇€瑕佺殑鍐呭, 瑙e喅depth瀛樺湪鐨勯棶棰?鏄剧ず鍑哄叏閮ㄧ殑鍐呭)銆€銆€

    authors = serializers.SerializerMethodField()
    def get_authors(self, obj):
        print(obj)
        author_query = obj.authors.all()
        return [{鈥榠d鈥?author.id, 鈥榥ame鈥?author.name} for author in author_query]

銆€銆€銆€銆€銆€銆€涓轰簡瑙e喅搴忓垪鍖栧拰鍙嶅簭鍒楀寲瀛樺湪鐨勫瓧娈典笉涓€鑷?/span>闂鐨勮В鍐虫柟妗?

    authors_info = serializers.SerializerMethodField()
    def get_authors_info(self, obj):
        authors_query = obj.authors.all()
        return [{鈥榠d鈥?author.id, 鈥榥ame鈥? author.name} for author in authors_query]

銆€銆€銆€銆€銆€銆€銆€銆€骞跺湪meta涓缃甧xtra_kwargs = {鈥樺搴旂殑瀛楁鈥?{write_only}: true}(鍙綔涓哄弽搴忓垪鍖栫敤)

銆€銆€銆€銆€

 

以上是关于92 搴忓垪鍖? 鍙嶅簭鍒楀寲的主要内容,如果未能解决你的问题,请参考以下文章

浜屽弶鏍戠殑搴忓垪鍖栦笌鍙嶅簭鍒楀寲

涓€涓父瑙佺殑鏍℃嫑闈㈣瘯棰橈細浜屽弶鏍戠殑搴忓垪鍖栧拰鍙嶅簭鍒楀寲

鏁版嵁搴忓垪鍖栨牸寮?YAML

springboot椤圭洰涓娇鐢╯pring-data-Redis瀵筸ap搴忓垪鍖栨椂鎶ラ敊

Dubbo 楂樺嵄婕忔礊锛佸師鏉ラ兘鏄弽搴忓垪鍖栨児寰楃ジ

鏁扮粍锛氬崌搴忓拰闄嶅簭