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
Post a Comment