I'm very new to Django, being a C guy (embedded/Linux) for the most part, so I apologize for my ignorance. I've tried searching for this exact query with no luck.
I have two Models, each of which has a many-to-many field pointing to the same third Model. This third model represents a restriction of some kind.
class Restriction(models.Model):
...
class ClassA(models.Model):
...
restrictions = models.ManyToManyField(Restriction)
class ClassB(models.Model):
...
restrictions = models.ManyToManyField(Restriction)
I anticipate having an instance of ClassA (call it object_a). I'd like to find all instances of ClassB which do not contain any of the restrictions in object_a. It would look something like this:
class ClassA(models.Model):
...
restrictions = models.ManyToManyField(Restriction)
def get_valid_b_objects():
return ClassB.objects.exclude(restrictions__in=self.restrictions)
But this doesn't feel right. Again, if any of the restrictions in a ClassB object is also in my ClassA object, that instance of ClassB should be excluded from the query. So, if:
Then object_a.get_valid_b_objects()
should return object_b4, object_b5, and object_b6.
Thank you very much. Feel free to direct me to another answer if this has already been addressed.
What you have should work. Just make sure you use an actual QuerySet
, not the related manager:
# don't forget '.all()'
return ClassB.objects.exclude(restrictions__in=self.restrictions.all())
This will exclude any B
instance that has any of the A
instance's restrictions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With