Questions tagged [django-annotate]
297 questions
0
votes
1 answer
Annotate with a static date - Django
I'm trying to annotate a queryset with a static date in Django.
With an integer (instead of a date) it works:
from django.db.models import Value, IntegerField
cars= Car.objects.all().annotate(sales=Value(0, IntegerField()))
How can I make it…

Maquisard
- 59
- 1
- 6
0
votes
2 answers
About converting custom functions in models into annotations in custom managers/querysets
Being new to Django, I'm starting to care a bit about performance of my web application.
I'm trying to transform many of my custom functions / properties which were originally in my models to querysets within custom managers.
in my model I…

Skratt
- 289
- 4
- 13
0
votes
1 answer
Annotate queryset with percentage grouped by date
Suppose I have the following model:
class Order(models.Model):
category = models.CharField(max_length=100, choices=CATEGORY_CHOICES, default=DEFAULT_CHOICE)
created_at = models.DateTimeField(auto_now_add=True)
I need to annotate an Order…

Vin
- 309
- 1
- 11
0
votes
1 answer
django postgresql - CASE types interval and integer cannot be matched
In Django I'm creating a queryset with CASE and annotation. It looks like this:
days_diff_calc = Case(
When(Q(sent_date__isnull=True),
then=None),
When(Q(received_date__isnull=True),
…

user2880391
- 2,683
- 7
- 38
- 77
0
votes
1 answer
Django - Annotate with count across ManytoMany relationships (refrase)
Django - Annotate with count across ManytoMany relationships
I am not able to find the way to annotate a queryset with a count of how many times an element is used in a many-to-many relationship.
class Profile(models.Model):
[...]
# Profile…

Daniel Aron Goldenberg
- 144
- 6
0
votes
0 answers
Django: Group by model parameter and Count
I have 2 models:
class FaultType(models.Model):
name = models.CharField(max_length=255, blank=False, default='')
visible = models.BooleanField(default=True)
And
class RunFault(models.Model):
run = models.ForeignKey(Run,…

Mormoran
- 751
- 13
- 34
0
votes
1 answer
How to use .annotate() or .aggregate() to extract a count from Django QuerySet relations?
I have the follow .model structure:
class ArtistWorkPlaceQuerySet(models.QuerySet):
def with_related(self):
return self.select_related('artist','work', 'place')
class ArtistWorkPlaceManager(models.Manager):
pass
class…

H C
- 1,138
- 4
- 21
- 39
0
votes
0 answers
How do I condense annotated queryset results based on a particular field?
Here's a fun one. Maybe someone can figure this out. Say I have a queryset something like the one below and want to get leads by month per company.
Company.objects.annotate(
month=TruncMonth('leads__date_received')
…

meesterguyperson
- 1,726
- 1
- 20
- 31
0
votes
1 answer
Django Annotate - How to list self instances?
I'm trying to do a Django annotate and want to list some of the object instances by filtering a field.
I have two model, which is Category and Article. This Category model has a field called super_category which is a choice field and the coices are…

Öykü
- 252
- 1
- 3
- 13
0
votes
0 answers
return type of annotate
I want to get date when first order was placed for each item in database.
If I use this:
event_queryset3 = OrderItemTable.objects.filter(filter).annotate(f_date=Min(f_date_ord))
this will give dates only for items which has been ordered.
If I use…

sandeep
- 721
- 1
- 7
- 14
0
votes
0 answers
How to group author's books annotation by kind?
I'm playing with the book/author django documentation.
Is there a way to get books from author grouped by kinds in an array agg? (pgsql)
The following query is correct but I need information from authors table.
…

user2652620
- 464
- 2
- 17
0
votes
1 answer
Django 2.1 - 'WhereNode' object has no attribute 'output_field' error
I am trying to filter some annotations in a ViewSetlike so:
queryset = Confirmation.objects.values('prediction__specimen_id').annotate(
sample_id=F('target_prediction__specimen_id'),
num_selected=Count('selected', filter=Q(selected=True)),
…

RubyJ
- 193
- 2
- 16
0
votes
1 answer
Perform trigonometric operations or any mathematical expression in Django annotate
I have a table which is having latitude and longitude fields.
`
location.objects.annotate( distance =math.fabs(math.pow((math.sin(
F('latitude') - float(90.378770)) /2 )),2) +
math.cos(90.378770) * math.cos(…

Ab1gor
- 398
- 3
- 19
0
votes
0 answers
Django annotation is giving two diffrent result in two cases
I have two cases:
Case 1:
qs = Stockdata.objects.annotate(sales_sum=Coalesce(Sum('salestock__quantity')))
qs2 = Stockdata.objects.annotate(purchase_sum=Coalesce(Sum('purchasestock__quantity_p')))
Case 2:
qs = Stockdata.objects.annotate(
…

Niladry Kar
- 1,163
- 4
- 20
- 50
0
votes
1 answer
How to extract INT from Queryset
I am trying to create a graph that dynamically updates based on date and count of POs.
Model:
class JobOrder(models.Model):
status = models.CharField('status', choices=STATUS, max_length=200, default="Waiting")
job_order =…

Martin Crespo
- 5
- 3