Questions tagged [django-annotate]

297 questions
0
votes
1 answer

How can I annotate django-polymorphic models that have GenericRelations to other models with GenericForeignKeys?

I have a parent model named Content that inherits from Django polymorphic. This is a simplified example, but I have a Post model that inherits from Content. On the Content model, notice that I have a GenericRelation(Note) named notes. What I'm…
0
votes
0 answers

Can I access a specific field value from an object through accessing by the related name, in Django?

I'm making a vote app (Django rest framework), where users can post content and have it voted on through two options. The Vote modelVote model code has a ForeignKey field to the Post model, with the related name attribute "votes". The vote model…
0
votes
0 answers

ORM Django annotate to convert string field to sorted list

I have a table containing a string field with a value like the following: '["value1", "value3", "value2"]'. Using the Django ORM, I require to convert that string type value to a list containing the values sorted alphabetically. It is necessary to…
Alonso
  • 1
0
votes
2 answers

Get average of multiple ratings values from different model instances (Django)

I'm working on this web-app that lets a project manager rate a vendor after finishing a task/job. Here is the models.py content: class Rating(models.Model): RATE_CHOICES = [ (1, 'Below Expectation greater than 0.02'), (2, 'Meet…
henoker
  • 99
  • 8
0
votes
0 answers

How to create nested serialization on a single model in Django Rest Framework?

I am working in DRF and trying to output nested serialized data from a single model. I am already using annotate() and getting the correct values for the data but I would like to go a step further and nest data if it is possible. I have a model…
0
votes
0 answers

Annotate on reverse many-to-many

I'm trying to work out why this doesn't work:- class A(models.Model): contacts = models.ManyToManyField(Contact) class Contact(models.Model): name = models.CharField() If I try and get a count of how many A there are with multiple…
bodger
  • 1,112
  • 6
  • 24
0
votes
0 answers

Django - Annotate queryset with result of function

I am trying to take a queryset and annotate it so that the value of the new field is the result of a function that requires the object being annotated. In this case I am taking a queryset of cars and annotating it with the distance of the car which…
Looble
  • 1
  • 1
0
votes
1 answer

How to delete everything but the latest object in each group in Django

I want to group my model objects by three fields, and delete all objects but the youngest for each group. My model: class DataFile(models.Model): filename = models.CharField(unique=True, max_length=256) timestamp = models.DateTimeField() …
0
votes
1 answer

Get extra field via annotate in Django many to many relation

I have a m2m relation between a Feature model and the User model through an intermediary table. Feature model represents all the available features, and a User can enable or disable zero, one or more of them via web or api. When a user enables a…
Paolo
  • 20,112
  • 21
  • 72
  • 113
0
votes
1 answer

Django - annotate the most frequent field taken from another model linked with foreignKey

I have a model of users and a model with a survey in which users express their opinion with a vote with an integer number that identified a particular color of eyes. A srcID user describes the color of the eyes of a dstID user according to his…
0
votes
0 answers

How to filter highest amount for each single date in Django?

Models: Customer Table class Customer(models.Model): name = models.CharField(max_length=100) email = models.CharField(max_length=30) phone = models.CharField(max_length=12) def __str__(self): return f"Customer : {self.name}" Order Table class…
0
votes
0 answers

Django annotate and aggregate not suport Timefild for sum

How can I make the total time sum of each user being that annotate and aggregate have no support for Timefild Models: class Pireps(models.Model): ... flight_time = models.TimeField(auto_now=False, auto_now_add=False, null=True) Views: def…
0
votes
0 answers

Doubled value when using annotate Sum,Count

I am using this code and I get a result which is not what I expect queryset= User.objects.annotate(earned_leave=Sum('logs__work_hours')) Here is my data "logs": { "id": 96, "time_in": "11:43:34", …
0
votes
0 answers

How can i count list of value from another model in Django?

I have two models and one of them contain a list of values that I want to count in another model. query1= list(Model1.objects.filter(CUSTOMER=customer.id).values_list("NAME",flat=True)) print(subquery) # [{'test1','test2,'test3','test4'}] response…
0
votes
0 answers

How does distinct parameter work with the Count method in annotate?

I got a problem with annotate method when I was using the Count method to count multiple columns that come from the database which have a relationship with one of the tables. Let me give you a quick example: match_session_instance =…
Abdelhamed Abdin
  • 556
  • 1
  • 6
  • 19