Home > Back-end >  DRF getting unneccessary data while querying
DRF getting unneccessary data while querying

Time:12-29

I have Category model and am querying through this model to list all categories. Im using django-silk to see how many queries got executed. It shows, category and Accounts. Accounts model has no relation with Catgeory. Below are the results.

enter image description here

It shows accounts on the list of queries enter image description here

Im not requesting any data from Accounts Model.

#Views.py

class ListCategory(generics.ListAPIView):
    queryset = Category.objects.all()
    serializer_class = CategorySerializer

#Serializer.py

class CategorySerializer(ModelSerializer):
    class Meta:
        model = Category
        fields = [
            "category_name",
            "slug",
            "title",
            "description",
            "category_image",
        ]

#Models.py


class Category(models.Model):
    category_name = models.CharField(max_length=15, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    title = models.CharField(max_length=25)
    description = models.CharField(max_length=300)
    category_image = models.ImageField(upload_to="photos/category")

    class Meta:
        verbose_name_plural = "Category"

    def __str__(self):
        return self.category_name

It's not only limited to this API, In all API it shows account. I have exactly no idea why this happens.

CodePudding user response:

If you are seeing a query to the Accounts model when querying the Category model, it is likely that the Accounts model is being accessed indirectly through a related model.

Check for foreign key relationships: If the Category model has a foreign key relationship to the Accounts model, or if the Accounts model has a foreign key relationship to another model that is related to the Category model, this may cause additional queries to be executed.

  • Related