Hemanth Sai Uppu
Hemanth Sai Uppu

Reputation: 31

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

Upvotes: 3

Views: 1186

Answers (1)

bedelturker
bedelturker

Reputation: 31

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.

Upvotes: 3

Related Questions