python - Django query with an aggregate function -


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 view want create list of topiclabels, have @ least 1 connection (i. e. hastopics returns true).

afaik impossible in django use instance methods in filter expressions (i. e. topiclabel.objects.filter(topiclabel.hastopics).order_by('order') impossible).

what correct (django-style) way implement such query (preferably database-independent) ?

for specific case, don't need aggregation function @ all. use isnull filter:

topiclabel.objects.filter(connection_label__isnull=false) 

for cases need aggregate, can filter on annotations described in aggregation documentation.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -