Django query with an aggregate function

Mentiflectax

I have following models:

class TopicLabel(models.Model):
    name = models.CharField(max_length=256)
    order = models.IntegerField(null=True, blank=True)
    def __unicode__(self):
        return self.name

    def hasTopics():
        return TopicLabelConnection.objects.filter(labelId=self.id).count() > 0

class TopicLabelConnection(models.Model):
    topicId = models.ForeignKey(Topic, related_name='connection_topic')
    labelId = models.ForeignKey(TopicLabel, related_name='connection_label')

    def __unicode__(self):
        return self.labelId.name + ' / ' + self.topicId.title

In a certain view I want to create a list of all TopicLabels, which have at least one connection (i. e. where hasTopics returns true).

AFAIK it is impossible in Django to use instance methods in filter expressions (i. e. something like TopicLabel.objects.filter(TopicLabel.hasTopics).order_by('order') is impossible).

What is the correct (Django-style) way to implement such query (preferably database-independent) ?

Peter DeGlopper

For this specific case, you don't need an aggregation function at all. Use an isnull filter:

TopicLabel.objects.filter(connection_label__isnull=False)

For cases where you do need an aggregate, you can filter on annotations as described in the aggregation documentation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Update Query with aggregate function

From Dev

Alternative query for MongoDB aggregate function

From Dev

Alternative query for MongoDB aggregate function

From Dev

SQL Update Query with aggregate function

From Dev

How to just retrieve the integer of a aggregate query in Django?

From Dev

Is it possible to GROUP BY an aggregate query with django ORM?

From Dev

How to just retrieve the integer of a aggregate query in Django?

From Dev

Is it possible to GROUP BY an aggregate query with django ORM?

From Dev

MongoDB nested query using aggregate function

From Dev

Query to get identifier and aggregate function value of a row

From Dev

sql query using aggregate function inside case

From Dev

SQL query aggregate function with two tables

From Dev

sql query using aggregate function inside case

From Dev

MongoDB nested query using aggregate function

From Dev

SQL query aggregate function in WHERE clause

From Dev

Django filter on annotation from custom aggregate function

From Dev

Django query to retrieve rows that meet aggregate criteria on related table

From Dev

"You tried to execute a query that does not include the specified aggregate function"

From Dev

Why is this T-SQL query throwing an "aggregate function" error message?

From Dev

How to optimize query containing two identical subqueries except for aggregate function?

From Dev

Reuse aggregate function value multiple times in the same query in sql

From Dev

Reuse aggregate function value multiple times in the same query in sql

From Dev

Can GROUP BY query have non aggregate function call in projection list?

From Dev

Why is this T-SQL query throwing an "aggregate function" error message?

From Dev

How to add a aggregate function where selected columns are within the same query

From Dev

How to join the result of different aggregate function in the same query?

From Dev

why is aggregate function not working with Inline view for below query?

From Dev

SQL Query with Aggregate function on Left Join of One-to-Many Relationship

From Dev

Aggregate query on time in MongoDB

Related Related

  1. 1

    SQL Update Query with aggregate function

  2. 2

    Alternative query for MongoDB aggregate function

  3. 3

    Alternative query for MongoDB aggregate function

  4. 4

    SQL Update Query with aggregate function

  5. 5

    How to just retrieve the integer of a aggregate query in Django?

  6. 6

    Is it possible to GROUP BY an aggregate query with django ORM?

  7. 7

    How to just retrieve the integer of a aggregate query in Django?

  8. 8

    Is it possible to GROUP BY an aggregate query with django ORM?

  9. 9

    MongoDB nested query using aggregate function

  10. 10

    Query to get identifier and aggregate function value of a row

  11. 11

    sql query using aggregate function inside case

  12. 12

    SQL query aggregate function with two tables

  13. 13

    sql query using aggregate function inside case

  14. 14

    MongoDB nested query using aggregate function

  15. 15

    SQL query aggregate function in WHERE clause

  16. 16

    Django filter on annotation from custom aggregate function

  17. 17

    Django query to retrieve rows that meet aggregate criteria on related table

  18. 18

    "You tried to execute a query that does not include the specified aggregate function"

  19. 19

    Why is this T-SQL query throwing an "aggregate function" error message?

  20. 20

    How to optimize query containing two identical subqueries except for aggregate function?

  21. 21

    Reuse aggregate function value multiple times in the same query in sql

  22. 22

    Reuse aggregate function value multiple times in the same query in sql

  23. 23

    Can GROUP BY query have non aggregate function call in projection list?

  24. 24

    Why is this T-SQL query throwing an "aggregate function" error message?

  25. 25

    How to add a aggregate function where selected columns are within the same query

  26. 26

    How to join the result of different aggregate function in the same query?

  27. 27

    why is aggregate function not working with Inline view for below query?

  28. 28

    SQL Query with Aggregate function on Left Join of One-to-Many Relationship

  29. 29

    Aggregate query on time in MongoDB

HotTag

Archive