Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serializer being called multiple times django python

This is code of serializer which is working perfectly fine.. But,serializer is being called multiple times

class StatisticLocationSerializer(serializers.one, two):
      domains = serializers.SerializerMethodField(read_only=True)
      statistic = serializers.SerializerMethodField(read_only=True)
      patents = serializers.SerializerMethodField(read_only=True)
      inventors = serializers.SerializerMethodField(read_only=True)

#### METHOD FIELDS ####
      def setup_eager_loading(queryset):
            return something


class Meta:
    model = models.LocationModel
    fields = ( 'domains', 'statistic',
              'patents')
    lookup_field = 'slug'
    extra_kwargs = {
        'url': {'lookup_field': 'slug'}
    }

##### THIS IS HOW I"M CALLING SERIALIZER CLASS ####
serializer_class = StatisticLocationSerializer
def get_queryset(self):
    queryset = self.get_serializer_class().setup_eager_loading(queryset)

NOTE: I CHECKED THE FUCTION get_queryset is also being called only Once

like image 981
Hemanth Sai Uppu Avatar asked Jul 31 '26 13:07

Hemanth Sai Uppu


1 Answers

Are you sending a request to your endpoint through the DRF user interface? I had the same problem with functions being called twice or thrice (init functions) with no explanation.

However, if you send a request to your endpoint via a service like Postman, the functions are being called once. So, it seems to be a problem with the DRF's own interface rather than the code itself.

For real world uses, your code should be fine.

like image 82
bedelturker Avatar answered Aug 02 '26 01:08

bedelturker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!