Reputation: 221
I have a query in Django that is resulting in unique rows but some contain common values in various fields. I want to consolidate those rows with the common values. Example: Assume each row returns an id, library, book title, author, and publication date. I only want to know only about the book title, author and publication date. Additionally, I would like to know about 1 and only one library where it can be found. How so using Django?
Upvotes: 0
Views: 161
Reputation: 4224
Could you try this:
Books.objects.all().only('title', 'author', 'date').extra(where=['library IS NOT NULL']).distinct()
Upvotes: 2
Reputation: 15206
Chances are your data model is incomplete. It sounds like you're trying to find a library which is in possession of a particular book. In the case where there are many libraries in possession of said book, you'll need some discriminating criteria in order to get the database to filter the list of libraries down to one and only one. Otherwise, if you really don't care, consider using the limit
method.
myquery.limit(1)
Upvotes: 0