Questions tagged [django-annotate]

297 questions
1
vote
1 answer

Django ORM, Sum column values through query

I have a table with the following values: user amount date John 10 2017-07-01 John 20 2019-07-01 John 30 2020-09-01 John 40 2021-11-01 ... ... ... Dan -20 2019-02-01 Dan -30 2020-04-01 Dan -40 2021-06-01 The input of the…
Saeed
  • 3,294
  • 5
  • 35
  • 52
1
vote
1 answer

Django annotate sum on fk_set

I'm trying to annotate sum of fields in related set: My models: class Report(models.Model): class ReportCommissionPosition(models.Model): report = models.ForeignKey(Report) reservation =…
Kihaf
  • 65
  • 1
  • 4
1
vote
1 answer

How to make annotation for foreign key in Django?

I need to replace _points property by annotation so that I have the opportunity to sort entries in the Django admin panel by this value. My model: class CustomUser(models.Model): inviter = models.ForeignKey('self', on_delete=models.SET_NULL,…
sasha
  • 135
  • 11
1
vote
2 answers

Update column in Django with child columns

I have 2 models Parent, Child class Parent(models.Model): id = Base64UUIDField(primary_key=True, editable=False) cost = models.DateTimeField(default=None, blank=True, null=True) class Child(models.Model): id =…
1
vote
1 answer

Annotate in django object using function

I have two models Parent, Child class Parent(models.Model): id = models.IntegerField(...) class Child(models.Model) id = models.IntegerField(...) parent = models.ForeignKey(Parent, ...) wanted = models.CharField(default="yes") I…
1
vote
2 answers

How to annotate Django Queryset with the same value but with a different data type of one of existing fields?

I have a Django model that has a NumberVal field which is models.FloatField(). Then I need to annotate the queryset objects like so .annotate(numberval_as_text=str(OuterRef("NumberVal"))) - the same value, but as a string except for the fact that…
1
vote
1 answer

Django- How to get element index in a queryset by using F expression

I have following models: class Topic(models.Model): title = models.CharField(max_lenght=32) # Some other fields. class Thread(models.Model): topic = models.ForeignKey(Topic, related_name=threads', on_delete=models.CASCADE) # some…
msln
  • 1,318
  • 2
  • 19
  • 38
1
vote
1 answer

Django Annotate and Aggregate

I'd like to Sum the post_value of all of the Posts for each post_user to eventually use in a chart. I'm struggling with how to formulate the query? So far, I've got to: user_totals =…
1
vote
0 answers

Django Concatenate charfields and parse it as Datetime

how are you? I have a model that has two fields named start_time and end_time, both are CharField, and this model has a relation to another model that has a field named date it is a DateField. It looks like this class A(models.Model): date =…
1
vote
1 answer

Django: check on model with manytomany field if object is in that list with filter

Let's say we have a Notification model with manytomany=ManyToManyField(User, blank=True) field with User model. How to annotate a field is_in_manytomany to return boolean with True and False only depending on a condition, whether self.request.user…
1
vote
0 answers

Django - Use annotation in query - Related Field got invalid lookup

In my manager, I've overriden get_queryset to annotate sqft_annotated value. class RealestateManager(models.Manager): def get_queryset(self): return super().get_queryset().annotate( sqft_annotated=Coalesce('sqft_a', 'sqft_b',…
Milano
  • 18,048
  • 37
  • 153
  • 353
1
vote
1 answer

how to annotate each object with random value

I want to have items with random annotations. I tried this: items = Item.objects.all().annotate(random_value=Value(randint(1,6)),output_field=PositiveIntegerField())) And it is random, but THE SAME for EVERY Item in QuerySet. But I want to have…
Tomasz Brzezina
  • 1,452
  • 5
  • 21
  • 44
1
vote
2 answers

Django boolean annotate returning duplicates

I have the following models: class Institution(models.Model): pass class Headquarter(models.Model): institution = models.ForeignKey(Institution, related_name='headquarters') class Audit(models.Model): headquarter =…
Johnny Beltran
  • 701
  • 2
  • 8
  • 22
1
vote
1 answer

annotate group by in django

I'm trying to perform a query in django that is equivalent to this: SELECT SUM(quantity * price) from Sales GROUP BY date. My django query looks like this: Sales.objects.values('date').annotate(total_sum=Sum('price * quantity')) The above one…
Eranki
  • 750
  • 1
  • 11
  • 30
1
vote
0 answers

How to execute a django query with group by, alongwith multiplying two columns?

I have data like this product type quantity price/quantity chocolate buy 2 100 chocolate buy 4 200 colddrink buy 3 300 chocolate sell 3 200 colddrink buy 1 100 now I want to group the data by product and its type, and the…