Re: Custom serializers

18 views
Skip to first unread message

Xavier Ordoquy

unread,
Aug 10, 2016, 2:50:25 AM8/10/16
to django-res...@googlegroups.com
Hi,

if you want to flatten a related object, you’ll have to use the source argument to map the serializer fields to a related object fields.
The downside here is that you’ll have to list explicitly all the related fields which can be somewhat tedious.

Regards,
Xavier Ordoquy,
Linovia.

Le 10 août 2016 à 02:04, Emre Yavuz <emre.y...@gmail.com> a écrit :


      Hello, i have problem in custom serialization and i look in documentation for hours, but i couldn't figure out what i will do. 
 I have nested serializers object like below but i want to have non-nested object, can anyone help me about this? 

Nested Object: 
{
        "id": 1,
        "adDate": "20-08-2016",
        "price": "30.50",
        "city": "Istanbul",
        "latitude": "28.987509",
        "longitude": "41.040353",
        "isPublished": true,
        "book": {
            "id": 1,
            "bookName": "GameOfThrones",
            "category": "Adventure",
            "description": "Adventure Book",
            "author": "Emre Yavuz",
            "language": "Sangridce",
            "publicationDate": "2023",
            "publisher": "Deu_Yapim",
            "edition": "22",
            "page_number": 900
        }
}
Non-Nested Object:
{
        "id": 1,
        "adDate": "20-08-2016",
        "price": "30.50",
        "city": "Istanbul",
        "latitude": "28.987509",
        "longitude": "41.040353",
        "isPublished": true,
        
        "id": 1,
        "bookName": "GameOfThrones",
        "category": "Adventure",
         "description": "Adventure Book",
         "author": "Emre Yavuz",
        "language": "Sangridce",
        "publicationDate": "2023",
        "publisher": "Deu_Yapim",
         "edition": "22",
         "page_number": 900
       
}
MY Serializer Class: 
 class AdvertSerializer(serializers.ModelSerializer):
    book = BookSerializer()
    class Meta(object):
        model = Advert
        fields = ('id', 'adDate', 'price',"city","latitude","longitude","isPublished",'book','seller')
        depth = 2
class BookSerializer(serializers.ModelSerializer):

    photo = serializers.ImageField(max_length=None,use_url=True) 
    class Meta(object):
        model = Book

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages