How to execute a "Group By" query in Django 1.1?

I have seen a lot of talk about aggregation 1.1, but I'm not sure how to use it to complete a simple group.

I am trying to use the Django Sitemap framework to create a sitemap.xml file that Google can crawl to find all the pages of my site. I am currently passing all objects as in Model.objects.all(), but all that really matters is that only 1 is transferred to the name. There may be 5-10 instances of the model with the same name, but I only want to pass them in order to avoid duplication.

If I do something like this:

Model.objects.values('name').annotate(Count('name'))

It gives me what I want, but then I do not extract all the fields from the model - then the code that creates the site map will be forced to re-request the creation of a link for each individual model. So, how do I get it to group by name when retrieving all the fields of the model?

+3
source share
2 answers

Django models are lazy loaded. This will be the same amount of overhead if your code matches your model relationship, as if you had a site map. Model fields are essentially proxy servers until you request the appropriate models.

+1
source

Maybe Distinct will help you?

Model.objects.values ​​('name'). Everything(). Distinct()

+1
source

Source: https://habr.com/ru/post/1720334/


All Articles