Questions tagged [django-annotate]
297 questions
0
votes
0 answers
Set value of annotate field from another related child value
I want to add extra field which with value from 2nd level of related model. the problem is because the first level is one to many.
example Models:
class ModelA(BaseModel):
name=models.CharField()
class ModelB(BaseModel):
…

Muhammad Sholeh
- 11
- 4
0
votes
2 answers
Counting the children matching grandchildren conditions in a single Django query
Given the following models:
class Flight:
class Checklist:
flight = ForeignKey(Flight)
class Item:
checklist = ForeignKey(Checklist)
completed = BooleanField()
I need to get the number of completed checklists for each flight. A…

user15656060
- 33
- 4
0
votes
0 answers
Resolve UUID to DangoObjectNode in Subquery
I've got a database with an simple Employee model and node in Django. I´m using Graphene to create an API around this that allows a user to retrieve the right data.
class Employee(models.Model):
id = models.UUIDField(primary_key=True,…

Yellow
- 3,955
- 6
- 45
- 74
0
votes
2 answers
Django annotate count of subquery items - ValueError: This queryset contains a reference to an outer query and may only be used in a subquery
I have an Agent, Client and Car models.
In Client model: agent = ForeignKey('Agent'...)
In Car model: client = ForeignKey('Client'...)
I want to annotate (on an Agent QuerySet) a total number of active cars of all clients of an agent.
So if the…

Milano
- 18,048
- 37
- 153
- 353
0
votes
0 answers
Intances are created when using annotate and Foreign Key
I have the following situation:
I am creating an app where I have a Person model and I want to store a status history in a table, so that looking for the most recent status in the table would return the current status of the person.
My solution was…

adonayE
- 1
- 1
0
votes
1 answer
Django annotate count for another queryset
Models:
class Regions(models.Model):
name = models.CharField(max_length=255, unique=True)
class Owners(models.Model):
name = models.CharField(max_length=255, null=False, unique=True)
url = models.URLField(null=True)
class…

Danza
- 23
- 4
0
votes
1 answer
annotation in admin list with many to many relation
I have a link between ads and products and stores, and I want to sort them in the admin by store:
class Ad(models.Model):
products = models.ManyToManyField(Product, blank = True)
device = models.ForeignKey(Display, on_delete =…

xtlc
- 1,070
- 1
- 15
- 41
0
votes
1 answer
Django: Perform GROUP BY over a view queryset
I need to perform group by over a queryset of objects already filtered by standard django filters in order to filter by how many objects in queryset are related to same foreign key object. My code now (does not work):
class…

Igor Atsberger
- 97
- 1
- 9
0
votes
1 answer
django annotate - issue with counting nested items in the template
I have a list of checkboxes and labels for business challanges. I'd like to apply categories for each business challange but I am struggling to count number of business challange in each category.
This is what I have:
models.py
class…

Adrian
- 725
- 4
- 18
0
votes
1 answer
Get Highest Likes User with Django
class Profile(models.Model):
..
goldcoin = models.IntegerField(default=0)
likes = models.ManyToManyField(User, blank=True, related_name='user_like')
dislikes = models.ManyToManyField(User, blank=True, related_name='user_dislike')
…

Smile Pumpkin
- 3
- 1
0
votes
1 answer
Django ORM annotate Query
I have a subquery which returns an array. I want to perform certain operations to pick one data out of that array. I am not able to figure out how to do it.
query_obj = MySecurity.objects.filter(
valid_data=True
).values(
…

shaswat kumar
- 369
- 8
- 19
0
votes
0 answers
Django treebeard count on each child
I'm using django-treebeard to create tree of Structures (some kind of folders).
Each Structure could contain a Resource. When I list my Structures I need to know how many Resources are in current Structure and it's children.
Is there a way to Sum…

Marshall
- 65
- 1
- 6
0
votes
0 answers
How to limit results with annotate and prefetch_related in django?
I'm have a django query with annotate and prefetch_related.
all_questions = Question.objects.all().annotate(is_complete=Case(
When(
pk__in=completed_questions.values('pk'),
then=Value(True)
…

Gagan
- 367
- 1
- 4
- 18
0
votes
1 answer
How to use OuterRef with _in filter
I've been trying to rework a query and I can't figure out the solution. Here is the setup (oversimplified):
An object OrderLine with a quantity and a product, and the product itself with a stock
class Product(models.Model):
inventory_quantity =…

Rfayolle
- 3
- 4
0
votes
1 answer
Annotating in django using a field from another model
I'm trying to calculate a field that it's a sum agregation when two of other fields are the same. The problem is that one of this fields is from the same model as the sum field, but the other one is from another model.
models.py:
class…

patatafly
- 144
- 10