Disable querying a field in Django Rest Framework

Here is the model class. Where categories is a nested mptt model tree.

class MyModel(models.Model):
    categories = models.ManyToManyField(Category) # to another nested model (self referncing tree)

The serialzier is a simple one

class MyModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = MyModel
        exclude = ['categories', ]

When serializing the Model object, on sql queries are executed to fetch categories. I dont want the categories field in the response, so I excluded it in the serializer but the categories table is queried when serializing.

serializer = MyModelSerializer(MyModel.objects.first())

How can I disable queries to that table when doing this particular operation? Thanks.

Back to Top