Questions tagged [django-subquery]

Questions about Django's Subquery expressions

Subquery expressions were added in Django 1.11.

30 questions
59
votes
7 answers

Django 1.11 Annotating a Subquery Aggregate

This is a bleeding-edge feature that I'm currently skewered upon and quickly bleeding out. I want to annotate a subquery-aggregate onto an existing queryset. Doing this before 1.11 either meant custom SQL or hammering the database. Here's the…
Oli
  • 235,628
  • 64
  • 220
  • 299
15
votes
2 answers

Django subquery and annotations with OuterRef

I'm having problems using annotate() with OuterRef in Django 1.11 subqueries. Example models: class A(models.Model): name = models.CharField(max_length=50) class B(models.Model): a = models.ForeignKey(A) Now a query with a subquery (that…
toucan
  • 1,465
  • 1
  • 17
  • 31
12
votes
3 answers

Django conditional Subquery aggregate

An simplified example of my model structure would be class Corporation(models.Model): ... class Division(models.Model): corporation = models.ForeignKey(Corporation) class Department(models.Model): division =…
Eldamir
  • 9,888
  • 6
  • 52
  • 73
11
votes
1 answer

Django subquery throws "more than one row returned by a subquery used as an expression"

I have recently upgraded to django 1.11 and I want to use the newly released, Subquery exrpession feature. There are two models as follows class Project(models.Model): title = models.CharField(_("Project Name"), max_length=128) class…
sajid
  • 807
  • 1
  • 9
  • 23
9
votes
2 answers

Using Subquery to annotate a Count

Please help me I've been stuck on this for way too long :( What I want to do: I have these two models: class Specialization(models.Model): name = models.CharField("name", max_length=64) class Doctor(models.Model): name =…
4
votes
1 answer

Django: Annotate list of related fields

I have an Company and User models with a related model CompanyRecruiter: class CompanyRecruiter(models.Model): organization = models.ForeignKey(Company, related_name="company_recruiters") recruiter = models.ForeignKey(User,…
3
votes
1 answer

Annotation with a subquery with multiple result in Django

I use postgresql database in my project and I use below example from django documentation. from django.db.models import OuterRef, Subquery newest =…
2
votes
3 answers

Django with MySQL: 'Subquery returns more than 1 row'

Using django with a MySQL DB and given these models: ModelB ---FK---> ModelA - ref_type - ref_id ModelC I want to get all the ModelC for each ModelA via an annotation. I tried many options looking at existing solutions but could not…
Michael
  • 8,357
  • 20
  • 58
  • 86
2
votes
1 answer

Django: "missing 1 required positional argument: 'lookup _name'" when it is not missing?

I am working with Django 3.1 ORM and am running (with pytest) a test involving a complex nested query. I get this failure: self = lookup = 'lte' def get_lookup(self,…
Lutz Prechelt
  • 36,608
  • 11
  • 63
  • 88
2
votes
1 answer

How to limit top N of each group in Django ORM by using Postgres Window functions or Lateral Joins?

I have following Post, Category & PostScore Model. class Post(models.Model): category = models.ForeignKey('Category', on_delete=models.SET_NULL, related_name='category_posts', limit_choices_to={'parent_category': None}, blank=True, null=True) …
2
votes
1 answer

Summarized Subquery annotation with a filter by ForeignKey outer reference

I try to code the equivalent Django query from this SQL query, but I'm stuck. Any help is welcome. I receive a race id and from this race I want to do some statistics : nb_race = the number of races from a Horse before the race given, best_chrono =…
Frederic
  • 115
  • 10
2
votes
2 answers

How to perform a double step Subqueries in Django?

I have the below models: class Group(models.Model): group_name = models.CharField(max_length=32) class Ledger(models.Model): ledger_name = models.CharField(max_length=32) group_name =…
Niladry Kar
  • 1,163
  • 4
  • 20
  • 50
1
vote
0 answers

Annotate GIS Intersection in Nested Subquery

I am trying to execute a complex Django query involving a nested subquery. Starting with the Stick model, I want to annotate the ownership_pct from the StickOwnership model (which is straightforward given the FK relationship). Next, I also want to…
Jeff Stewart
  • 97
  • 10
1
vote
1 answer

Performing a Subquery, Sum and Join in django ORM

I have 2 django models which aren't linked by ForeignKey due to legacy system. class Parent(model): name -> CharField() class Child(model) parent_name -> CharField() cost -> IntegerField() I want to achieve a left join which…
th3465
  • 31
  • 5
1
vote
1 answer

Updating fields via subquery in Django

I have an app with models and db schema that looks as shown below. I am trying to add field r to L2 in order to be able to access the related objects from model R. The new field is not shown in the schema figure. Retrieving the desired value of…
m000
  • 5,932
  • 3
  • 31
  • 28
1
2