Django Annotate - get list of distinct field values

rahul

I have 3 models - Book, Page and Language.

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100)

class Page(models.Model):
    image = models.ImageField(upload_to='Uploaded_images')
    language = models.ForiegnKey(Language, models.CASCADE)
    book = models.ForeignKey(Book, models.CASCADE)

class Language(models.Model):
    name = models.CharField(max_length=20)

There's a one-to-many relationship between Book and Page objects. Some pages in a book can be of a different language, so I added the ForeignKey in Page model and not in the Book model.

I want to get the list of all books along with the languages its pages were written in.

I tried this:

books = Page.objects.annotate(
        name=F('book__title')
    ).values(
        'name', 'book_id'
    ).distinct(
    ).annotate(
        page_count=Count('id'),
    ).values(
        'name', 'book_id', 'language'
    )

If there are 2 languages in the book, I get 2 entries for the same book title. I just need 1 entry for the book and be able to show its languages as a list.

Ex: If there's a book with pages in either of the 2 languages, Spanish and English, I'd like to extract the book title and the languages in it as ['English', 'Spanish'].

Is this doable?

Ngoc Pham

If you use Postgres, maybe you need start get data from class Book and use ArrayAgg.

Link: Django queryset annotate field to be a list/queryset

If you use rest-framework, you can use Serializer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Django annotate count with a distinct field

Django - How to annotate count() of distinct values

Django - Annotate Count() of distinct values grouped by Date

MongoDB aggregate get distinct values of field and output list of another field

Django queryset annotate field to be a list/queryset

Django ORM: distinct and annotate

Get a list of distinct values in List

Django retrieve get_foo_value within values_list and annotate query

Annotate a django field

Django annotate on boolean Field

Get extra field via annotate in Django many to many relation

Django: Is it possible to get field values for a list of objects in bulk?

Ordering django field values in a list

Get a list of distinct values from map

Get DISTINCT list of values within GroupBy sql

Get Distinct List of Values from Nested Object

How to get only distinct values from a list?

Django annotate whether field is null

Mongodb, get distinct values of a field and output the values as single document

Django: NotImplementedError: annotate() + distinct(fields) is not implemented

ORM to the distinct values from a model based on the latest datetime field in Django

How to get the count of distinct values in a column(field) in an elasticsearch index

Django: Annotate list of related fields

group a list and only get distinct values when input is a list

Django queryset where a field values are in a list

How to convert a List of duplicate values to a List of distinct values with respect to a field of T class

django queryset annotate all field in childtable

Django annotate field value from another model

Why django group by wrong field? annotate()