Solved: How to use %22to_representation%22 hook for django rest serializers

The main problem is that the %22to_representation%22 hook is not implemented in the 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             ๐Ÿ™‚ ๐Ÿ™‚ ๐Ÿ™‚ ๐Ÿ™‚ ๐Ÿ™‚ ๐Ÿ™‚ ๐Ÿ™‚ ๐Ÿ™‚             &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&gt;&gt;&gt;&gt;&gt;           &amp; &amp;; &amp;; &amp;; &amp;;           { { { { { { {            } } } } } }            [ [ [ [ [             ] ] ] ] ]                                 | | | |                ~ ~ ~                 ` ` `                  , , ,                   - - -                    / / /                     ' '                       " "                         # #                          $ $                           % %                            &amp;" "                             '( )                              * + , -                       ./ 0 1 2 3 4 5 6 7 8 9                        : ; &lt;"= ? @ 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 ๐Ÿ™‚ ๐Ÿ™‚ ๐Ÿ™‚ ๐Ÿ™‚ ๐Ÿ™‚ ๐Ÿ™‚ <<<” ” ” ” ” &; &; &; { { { { } } } [ [ [ ] ] ] ~ ~ ‘ ‘ # # $ $ % % &” ” ‘( ) * + ./ 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 { | } ~ โŒ‚ ร‡รผรฉรขรคร รฅรงรชรซรจรฏรฎรฌร„ร…ร‰รฆร†รดรถรฒรปรนรฟร–รœรธยฃรกรญรณรบรฑร‘ยชยบยฟยฎยฌยฝยผยกยซยปโ–‘โ–’โ–“โ”‚โ”คโ•กโ•ขโ•–โ••โ•ฃโ•‘โ•—โ•โ•œโ•›โ”โ””โ”ดโ”ฌโ”œโ”€โ”ผโŽ›โŽโŽ โŽžโŽ›โŽโŽ โŽž
The code is trying to add a field called ‘full_name’ to the UserSerializer that is not part of the model. It does this by overriding the get_fields() method and adding the full_name field to the fields dictionary.

Serializers rest framewords

Serializers are a class of objects that can be used to persist data in a model. Serializers can be used to transform data into a format that can be stored in a database.

What are hooks in Django

A hook is a function in Django that allows you to run code at certain points in the processing of a request. Hooks are useful for performing custom processing on incoming requests, or for handling specific situations that may arise.

Related posts:

Leave a Comment