1

I have an api which returns the material_id,material_name and it's store_id (foreign key) and store_name and it also has a search backend. So i want to return all the material_name and material_id's on one key material and similarly all the store fields in the key store.

So i've tried the code below

class MaterialSuggestions(generics.ListAPIView):
    search_fields = ['name', 'shape', 'color', 'material_type', 
                     'surface_type', 'store__name']
    filter_backends = (filters.SearchFilter,)
    queryset = Material.objects.all()
    serializer_class = MaterialSuggestionsSerializer 



class MaterialSuggestionsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Material
        fields = ('material','store')


    material =  serializers.SerializerMethodField('get_material')
    store = serializers.SerializerMethodField('get_store')


    def get_material(self,obj):
        return {'material_id':obj.id,'material_name':obj.name}


    def get_store(self,obj):
        return {'store_id':obj.store.id,'store_name':obj.store.name}

when i call the api i get something like this:

    {
        "material": {
            "material_id": 14,
            "material_name": "test"
        },
        "store": {
            "store_id": 28,
            "store_name": "test1"
        }
    },
    {
        "material": {
            "material_id": 13,
            "material_name": "test3"
        },
        "store": {
            "store_id": 29,
            "store_name": "test2"
        }
    }
] 

This is what I would ideally want to return.

    {
        "material": [ {
                       "material_id": 14,
                       "material_name": "test"
                      },
                     {
                      "material_id": 13,
                      "material_name": "test3"
                     }         
                   ]


          "store": [  {
                       "store_id": 28,
                       "store_name": "test1"
                      },
                      {
                       "store_id": 29,
                       "store_name": "test2"
                      }
                  ]
    } 

or Even this would be great

    {
        "material":  {
                       "material_id": 14,13
                       "material_name": "test","test3"
                      },       



          "store":   {
                       "store_id": 28,29
                       "store_name": "test1","test2"
                      },


    } 

How would we manipulate the data we are returning using a serialzer and how can we access the queryset which is going into the serializer ?

keshav N
  • 141
  • 1
  • 3
  • 10

1 Answers1

1

Your desired outputs are no longer really model serializers as for example you completely loose the relationships between materials and stores.

You should instead consider building your own dictionary, then converting it to a custom json and just pass it as a response as explained here: https://stackoverflow.com/a/35019122/12197595

alfajet
  • 389
  • 1
  • 14