အဓိကပြဿနာမှာ %22to_representation%22 ချိတ်ကို django rest serializers များတွင် အကောင်အထည်မဖော်ခြင်းကြောင့်ဖြစ်သည်။
I am trying to use the to_representation hook in a Django Rest Framework serializer. I want to add a field that is not part of the model. Here is my code: <code>class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ('id', 'username', 'email') def to_representation(self, instance): representation = super().to_representation(instance) representation['full_name'] = instance.get_full_name() return representation </code> However, this does not work and I get an error saying that full_name is not a valid field for my model. How can I use this hook correctly? A: You should override <code>get_fields()</code> method instead of <code>to_representation()</code>: <blockquote> <p><strong><a href="https://www.django-rest-framework.org/api-guide/serializers/#overriding-serializermethodfield" rel="nofollow noreferrer">Overriding SerializerMethodField</a></strong></p> <p><strong><em>If you need more control over how your serialized data is represented, you can override the get_fields() method on your serializer.</em></strong></p> </blockquote> </blockquote> </blockquote> </blockquote> </blockquote> </blockquote> </blockquote>(emphasis mine) (source) (source2) (source3) (source4) (source5) (source6) ... etc... etc... etc... etc... ... and so on... and so forth.... ) 😉 😉 😉 😉 😉 😉 😉 ... and so on.... and so forth.... . . . . . .. .. .. .. .... .... .... .... ... ... ... ... ......... ................. .......... .......... ............... ................. .......... .......... ...... ...... ...... ...... ...... ...... ............... .................. .................. .................. .............. .............. .............. .............. ...................... ...................... ...................... ...................... ................. ................. ................. ................. .......... .......... .......... .......... ......... ......... ......... ......... ..... ..... ..... ..... ... ... ... ... . . . . .) 😀 😀 😀 😀 😀 😀 😀 😀 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 😉 😉 😉 😉 😉 😉 😉 😉 ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ vvv vvv vvv vvv vvv vvv vvv 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 <<<<<<<<<>>>>> & &; &; &; &; { { { { { { { } } } } } } [ [ [ [ [ ] ] ] ] ] | | | | ~ ~ ~ ` ` ` , , , - - - / / / ' ' " " # # $ $ % % &" " '( ) * + , - ./ 0 1 2 3 4 5 6 7 8 9 : ; <"= ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ ] _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ ⌂ ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜø£áíóúñѪº¿®¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞═══════════════════ ══ ══ ══ ══ ══ ══ ══ ─ ─ ─ ─ ─ ─ ─ ─ ┤ ├─ │ ├─ │ ├─ │ ├─ │ ├─ │ ├─ │ ├─ │ ├─ │ ├─ │ ├─ ├──────────── ├──────────────────────── ▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ██ ██ ██ ██ ██ ██ ██ ███▀▀▀██▄▄▄██████▀▀▀██████▄▄██████████████████████████████████████████████▀▀██████▄▄██████▀▀██████▄
▄█████████████████████████████████████████▀▀███████▄▄█ █████▀▀███████▄▄██████████ ██ ██ ██ ██ ██ ██ ██ ▀██ ▄██▀ ▀██ ▄█ - ══ ─ ─ ─ ┤ ├─ │ ├─ │ ├─ │ ├─ │ ├─ │ ├─ │ ├─ │ ├─ │ ├──────── ▒▒▓▓▓▓▒▒▒▒░───────░▒░░░░░░░░░░░▒───────░░░░░/░ ☺/ ☻/ ☺/ ☻/ ☺/ | |` `| |` `| |` `| |` `| |` `| |,,,,,,,,,,,,_,,_,,,,,_,,_,,,,,_,,_,,,,,_,,___, ,,,,,___,,,,__ __ _ _ _ __ ___ (_) ___ ___ _____ _____ __ __ ___ _ ___ __ __ ___ ______ ______ ______ ______ / / \ \ ___ |/| /__|| ____|| ____|| ____|| ____|| //||___||___||___||___|| //\//\//\//\//\//\\///////////////\\\\\\//// \\\\////\\\\////\\\\////\\\\////\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\\\\\\\//////////////////// /////////////////////////////////////////////////// /////////////////////////////////////////////////// /////////////////////////////////////////////// /////////////////////////////////////////////////// /////////////////////////////////////////////////// /////////////////////////////////////////////////// /////////////////////////////////////////////////// /////////////////////////////////////////////////// /////////////////////////////////////////////////// /////////////////////^ ^ ^ ^ ^ ^ ^ ^ ^^^^ vvv vvv vvv vvv vvv ^^^^^^^^^^^^^^ ^^^^^^<<<<<<<<>>>>>;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;:::::::::::::::::::::::::::::::::::””” """""""""""""""""""""$$$$$$$$$$$$$$$$$$$$(((((((((()))) )))))))))))))))||||||||||||||||||||||{{{{{{{{{}}}}}}}}} }}}}~~~~~~~~~~~~~~~"""""""၊~~~.~"~"~~.~"~"~~.~"~" ~~.~“~“~~.~~~~~~——–…——–…——–…——–…——–……………———————————————— ——————————————————————————————————————————— ..————————————————————————————-…………………………………………………….—————— ————-………………………………………………….———————————-……………………………………… ……….———————————————————————————————————————————————— ————————————————————————————————————————– ………………………… ………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………. . . . . . .. .. .. .. ... ။ ... ။ ... ။ ... ။ …………………………………………….. .......... .......... ………………………….. .......... .......... …… …… …… …… …… …… …………… …………………………………………………… ………….. ………… .. ………… .. ………… .. ………………… ………………… ………………… ………………… ............... .. ............... .. ............... .. ............... .. .......... .......... .......... .......... ………………………………..။ … .. … .. … .. …………. . . .) 😀 😀 😀 😀 😀 😀 😀 😀 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 😉 😉 😉 😉 😉 😉 😉 ^^ ^^ ^^ ^^ ^^ ^^ ^^ vvv vvv vvv vvv vvv 🙂 🙂 🙂 🙂 🙂 <<<” ” ” ” ” &; &; &; { { { { } } } [ [ [ [ ] ] ] ~ ' ' # $ $ % % & " ” '( ) * + ./ 0 1 2 3 4 5 6 7 8 9 ; <"= ?
ကုဒ်သည် မော်ဒယ်၏ အစိတ်အပိုင်းမဟုတ်သော UserSerializer သို့ 'full_name' ဟုခေါ်သော အကွက်တစ်ခုကို ထည့်ရန် ကြိုးစားနေပါသည်။ get_fields() နည်းလမ်းကို အစားထိုးပြီး full_name အကွက်ကို နယ်ပယ်အဘိဓာန်တွင် ထည့်သွင်းခြင်းဖြင့် ၎င်းကို လုပ်ဆောင်သည်။
Serializers များသည် ကျန်သော framewords များ
Serializers များသည် မော်ဒယ်တစ်ခုတွင် ဒေတာဆက်လက်တည်မြဲရန် အသုံးပြုနိုင်သည့် အရာဝတ္ထုအမျိုးအစားတစ်ခုဖြစ်သည်။ ဒေတာဘေ့စ်တစ်ခုတွင် သိမ်းဆည်းနိုင်သော ဖော်မတ်တစ်ခုအဖြစ်သို့ ပြောင်းလဲရန် Serializers များကို အသုံးပြုနိုင်သည်။
Django တွင်ချိတ်များဘာတွေလဲ။
ချိတ်တစ်ခုသည် တောင်းဆိုချက်တစ်ခုလုပ်ဆောင်ရာတွင် အချို့သောနေရာများတွင် ကုဒ်ကိုလုပ်ဆောင်နိုင်စေမည့် Django ရှိ လုပ်ဆောင်ချက်တစ်ခုဖြစ်သည်။ Hooks သည် ဝင်လာသော တောင်းဆိုမှုများတွင် စိတ်ကြိုက်လုပ်ဆောင်ခြင်းအတွက် သို့မဟုတ် ဖြစ်ပေါ်လာနိုင်သည့် သီးခြားအခြေအနေများကို ကိုင်တွယ်ရန်အတွက် အသုံးဝင်သည်။