[Django]-Django Rest Framework: Use URL parameter in serializer

3πŸ‘

βœ…

We would be required to update the code at two places.

  1. DetailView in views.py.

In this, we are updating context to include data_from. (please note, we can also access this directly in the serialzier)

class DetailView(APIView):
 ...
    def get(self, request, machine_name):
        application = Application.objects.get(machine_name=machine_name)
        serializer = OsdSerializer(application, context={
           "date_from": request.query_params.get("from")
        })

        return Response({"Everything you need to analyzing "+application.name: serializer.data})
 ...
  1. OsdSerializer in the serializers.py
class OsdSerializer(serializers.ModelSerializer):

   bands = BandSerializer(source='indice_to_use.needed_bands', many=True)
   satellite = SatelliteSerializer(source='indice_to_use.satellite_to_use')
   indice = IndiceSerializer(source='indice_to_use')
   alternate_name = serializers.SerializerMethodField()   

   def get_alternate_name(self, obj):
        return self.context.get('date_from')

   class Meta:
        model = Application
        fields = ['machine_name', 'name', 'description', 'indice', 'satellite', 'bands', 'date_from', 'alternate_name']
   

Another approach would be to access the request object directly from the context of the serializer. By default, the serializer context contains the request object in it. To do so, just update the serializer as mentioned below

OsdSerializer in the serializers.py

class OsdSerializer(serializers.ModelSerializer):

   bands = BandSerializer(source='indice_to_use.needed_bands', many=True)
   satellite = SatelliteSerializer(source='indice_to_use.satellite_to_use')
   indice = IndiceSerializer(source='indice_to_use')
   alternate_name = serializers.SerializerMethodField()   

   def get_alternate_name(self, obj):
        return self.context.get('request').query_params.get('from', None)
  
   class Meta:
        model = Application
        fields = ['machine_name', 'name', 'description', 'indice', 'satellite', 'bands', 'date_from', 'alternate_name']
   
πŸ‘€VJ Magar

2πŸ‘

First you need to change from accessing the parameter from kwargs to request.GET instead.

If you have a url like this in django '/user/<int:user_id>' then you access through kwargs.

But if you send a url parameter, like this '/user/?user_id=1'. You access through the request object.

In your situation, I think rest_framework will send the request to the serializer automatically. So you can do something like this:

def get_alternate_name(self, obj):
    date_from = self.request.GET.get('from')

0πŸ‘

date=self.context.get('request').parser_context.get('kwargs').get(
        'edate')
πŸ‘€Vishnu T s

Leave a comment