In Django how can I get a queryset of all the records that have a particular reverse relationship? -


so, have data model simplified this

class activity(models.model):     start_date = models.datefield()     title = models.textfield()     ...  class outcome(models.model):     title = models.textfield()     code = models.charfield(max_length=20)     ...  class activityoutcome(models.model):     note = models.textfield()     outcome = models.manytomany(outcome)     activity = models.manytomany(activity)  class organisation(models.model):     title = models.textfield()     outcomes = models.manytomanyfield(outcome, related_name='outcome_organisation')     ...  class programme(models.model      title = models.textfield()     outcomes = models.manytomanyfield(outcome, related_name='outcome_programme')     ... 

i want have single class of activityoutcome because makes simpler process, need have programme outcomes , organisation outcomes. if wanted queryset of "programme" outcomes, how that?

you can use __isnull filter outcomes have related programme.

outcomes = outcome.objects.filter(outcome_programme__isnull=false) 

if more 1 programme can have same outcome, might need use distinct() prevent duplicates.

outcomes = outcome.objects.filter(programme__isnull=false).distinct() 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -