12👍
If you need all the distinct names, just do this:
Foo.objects.values('name').distinct()
And you’ll get a list of dictionaries, each one with a name key. If you need other data, just add more attribute names as parameters to the .values() call. Of course, if you add in attributes that may vary between rows with the same name, you’ll break the .distinct().
This won’t help if you want to get complete model objects back. But getting distinct names and getting full data are inherently incompatible goals anyway; how do you know which row with a given name you want returned in its entirety? If you want to calculate some sort of aggregate data for all the rows with a given name, aggregation support was recently added to Django trunk and can take care of that for you.
- [Django]-Posting complex data dictionary with request python
- [Django]-How to return most popular items in a table, but where each item is unique?
- [Django]-Celery Django not Registering Tasks, Module not found error in shell
2👍
this will not work because every row have unique id. So every record is distinct..
To solve my problem i used
foo = Foo.objects.all()
foo.query.group_by = ['name']
but this is not official API.
- [Django]-How do you produce/create tables in Geraldo Reports?
- [Django]-Django paginate the original search result
- [Django]-Command not found: django-admin (using anaconda 3.2.4 and pyenv)
- [Django]-Refactor this Python code to iterate over a container
- [Django]-How to solve "No Python interpreter configured for the module"? in Android Studio?