Questions tagged [django-annotate]

297 questions
1
vote
1 answer

annotating by one-to-many relationship in Django

Let's say I have these two models: class Test1: ........... class Test2: test1 = models.ForeignKey(Test1, related_name = 'tests') isCompleted = models.BooleanField() and I want to make this query: queryset =…
th3plus
  • 161
  • 2
  • 11
1
vote
1 answer

Find annotated list of siblings in Django

I have a simple database with two models that define a parent-child relationship. In it, a child can have two possible gender, "Male" or "Female". class Parent(models.Model): id = models.UUIDField(primary_key=True, editable=False, unique=True,…
Yellow
  • 3,955
  • 6
  • 45
  • 74
1
vote
1 answer

python django 'QuerySet' object has no attribute 'objects'

I am building a website using Python django. But, an error occurs while loading data using django ORM What's the problem? model.py class contact(models.Model): class Meta: db_table="contact" target = models.CharField(max_length=15) …
1
vote
1 answer

Rendering highest integer value from three models instances in django

I have three models which are related all to one model. class MyModelParent(models.Model): name = models.CharField(max_lenght=36, blank=True) def __str__(self): return self.name or "" class MyFirstModel(models.Model): …
1
vote
0 answers

How to use GROUP BY in Django without using values()?

I am trying to GROUP BY with Django ORM: sub = ( Participant.objects .values('category') .annotate( average=Avg(F('price')), ) ) It works as expected BUT the queryset does not contains instances of…
znat
  • 13,144
  • 17
  • 71
  • 106
1
vote
1 answer

Django - Annotate sum of duration in hour

I have a field Time with type DurationField and I want to sum the values and convert it into decimal hour Task.objects.all().annotate(total=Sum('Time')) For example if total = timedelta(seconds=5400) I want to get 1.5 hours
MichaelS
  • 259
  • 2
  • 13
1
vote
1 answer

Python group a list of years, months to remove duplication of years

I have a table that contains orders, in which contains the date column. I am getting back the aggregate of the years, and months from that column so that I can use that data in a filter on the front end. I have managed to get this data back, however…
Ross
  • 2,463
  • 5
  • 35
  • 91
1
vote
1 answer

Django query annotate after filter doesn't work correctly

I have two models that are related using a m2m relationship through an intermediate model: class Chat(models.Model): participants = models.ManyToManyField(settings.AUTH_USER_MODEL, through="Join",…
aaronn
  • 448
  • 1
  • 6
  • 16
1
vote
1 answer

How to get the Primary key of annotate Count

Hi stackoverflow community, my question is about django annotate. Basically what I am trying to do is to find duplicated value with same values from two different fields in two different tables. This is my models.py class Order(models.Model): …
stranger
  • 134
  • 1
  • 7
1
vote
1 answer

Django annotate() is not adding up the number of entries but is instead repeating them

Background: The Amazon Kindle PaperWhite stores the words we lookup while reading into a sqlite3 database called vocab.db. I am working on a small kindle companion app that takes this db file and imports it into a django table for various…
user12758446
1
vote
1 answer

Django - How to add annotation to queryset for request.user

I want to add new field to queryset (by annotate()) according to request.user. I have following models: class Tweet(models.Model): ... class Like(models.Model): tweet = models.ForeignKey(Tweet, related_names='likes') user =…
msln
  • 1,318
  • 2
  • 19
  • 38
1
vote
1 answer

Advanced filtering for many-to-many annotations

I have following models: class CloudObjects(models.Model): object_id = models.AutoField(primary_key=True) object_name = models.CharField(max_length=256) creation_time = models.DateTimeField() removed_date =…
Marek
  • 82
  • 6
1
vote
1 answer

format count integer into percentage inside annotate in Djnago rest

I have to send the percentage of counts in the API call in DRF. I have calculated the counts and attaches to the queryset using annotate. But actually I need the percentage rather than the count. TYPES = ((1,'cold'), (2,'humid'), …
Reactoo
  • 916
  • 2
  • 12
  • 40
1
vote
2 answers

Annotating on a distinct Django Queryset is no longer using the distinct queryset

I have a query and I am trying to annotate the count of each value for the field tail_tip. My original query filters on a related table so it is necessary to use distinct(). I'm not exactly sure how to describe but it appears when I annotate the…
Dan S.
  • 167
  • 1
  • 3
  • 15
1
vote
1 answer

Concatenate foreign key fields with delimiter Django

Lets say I have 2 models with a foreign key relation which are used to bundle books: class Bundle(models.Model): name = models.CharField(max_length=100) class Book(models.Model): name = models.CharField(max_length=20) isbn =…
S.D.
  • 2,486
  • 1
  • 16
  • 23