2

I am trying to implement Nested serializer for the first time in django-rest-framework My requirement is i need the response data in below form :-

{
id: 0,
title: "Fresh",
data: [
    [{
            image: ".../assets/images/banana.jpeg",
            name: "Fruits",
            image_scaling: 2,
            API: "http://database_api"
        },
        {
            image: "../assets/images/banana.jpeg",
            name: "Vegetables",
            image_scaling: 2,
            API: "http://database_api"
        },
        {
            image: "../assets/images/banana.jpeg",
            name: "Exotic Vegetables",
            image_scaling: 2,
            API: "http://database_api"
        }
    ]
]

}

I am validating the token for the get api and listing the objects form DB. My current implementation of views.py is :-

class home(APIView):

 def get(self,request,format=None):

    header_token = request.META.get('HTTP_AUTHORIZATION', None)
    if header_token is not None:

        token = header_token
        access_token=token.split(' ')[1]
        print(access_token)
    
    if(validate_token(access_token) == None):
        raise NotFound('user does exist')
        

    queryset=Fresh.objects.all().select_related('data')
    print(queryset)
    serializer = FreshSerializer(queryset,many=True)
  
    return Response(serializer.data)

Serializer.py is :-

class DataSerializer(serializers.ModelSerializer):
   class Meta:
    model=data
    fields = ('name','image_scaling','image')

class FreshSerializer(serializers.ModelSerializer):
   data=DataSerializer(read_only=True, many=False)
   class Meta:
      model = Fresh
      fields = ('title','data')

models.py is :-

class data(models.Model):
  name=models.CharField(max_length=9,null=True)
  image_scaling=models.IntegerField(default=0,null=True)
  image=models.URLField(max_length = 200)
def __str__(self):
    return str(self.name)

class Fresh(models.Model):
   title=models.CharField(max_length=9)
   data=models.ForeignKey(data,on_delete=models.CASCADE,default=None, null=True)
def __str__(self):
    return str(self.title)+" " +str(self.data)

My current output is :-

[
{
    "title": "vegetable",
    "data": {
        "name": "vegetable",
        "image_scaling": 2,
        "image": "https......jpg"
    }
},
{
    "title": "exoticveg",
    "data": {
        "name": "exoticveg",
        "image_scaling": 2,
        "image": "https://.......jpg"
    }
} ]

I was trying to do by listapiview and modelviewset, but i am not able to get the desired output.I have seen similar queries in stack-overflow, but no solution seems to work for me. I am not getting list of objects of foreign key , am i doing something wrong in my model or running wrong query? I am really stuck at it. How shall i go about it. Please help!!!

1 Answer 1

3

i think you designed your models in wrong way:

in your desired json, every fresh has many data, so you model should be like this:

class data(models.Model):
  name=models.CharField(max_length=9, null=True)
  image_scaling=models.IntegerField(default=0, null=True)
  image=models.URLField(max_length=200)
  data=models.ForeignKey(
        Fresh, on_delete=models.CASCADE, related_name="fresh_data",
  )

def __str__(self):
    return str(self.name)

class Fresh(models.Model):
   title=models.CharField(max_length=9)

def __str__(self):
    return str(self.title)

and your serializer should be something like this:

class DataSerializer(serializers.ModelSerializer):
   class Meta:
    model=data
    fields = ('name', 'image_scaling', 'image')

class FreshSerializer(serializers.ModelSerializer):
   data = serializer.SerializerMethodField(read_only=True)
   class Meta:
      model = Fresh
      fields = ('id', 'title', 'data')


   def get_data(self, obj):
    return DataSerializer(obj.fresh_data, many=True).data
Sign up to request clarification or add additional context in comments.

2 Comments

yes you are right ,thank you so much for taking out time for my question :) i figured out problem in models and corrected them but still not able to get data as list of list , as my front end is in react native, and i need to send the data component in list of list format , can i write this logic in create() method of serializer?
why you can't get list of lists? for creating objects personally i would validate my data with serializer and use bulk_insert in views.py file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.