django - Trying filter query depending on a method of a related model -


class course(models.model):     def is_active(self):       return self.enroll_set.count()>0  class courseevent(models.model):     course = models.foreignkey(course) 

i want find events point active courses. like:

events = courseevent.objects.filter(course.is_active=true) 

thank you

the answer here: https://docs.djangoproject.com/en/dev/ref/models/querysets/#annotate

you make annotation, it'll filterable field.

it may this:

courseevent.objects.annotate(is_active=count('course__enroll')).exclude(is_active=0) 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -