Django request for containment of many-to-many

Is there a way to request containment of a subset or superset with many-to-many fields?

Suppose that each Man has a list of birds that they want to see, and each Driver has a list of birds. How can I make a query to find for this instance of Person, which wolves can each bird in the list of faces? And similarly, for this instance of Person, how do I find which aviaries have only birds in the list of people (but not all of them are necessary).

Here are my Django 1.5 models:

class Bird(models.Model):
    name = models.CharField(max_length=255, unique=True)

class Aviary(models.Model):
    name = models.CharField(max_length=255, unique=True)
    birds = models.ManyToManyField(Bird)

class Person(models.Model):
    name = models.CharField(max_length=255, unique=True)
    birds_to_see = models.ManyToManyField(Bird)

I know how to find aviaries that have at least one of the birds, but I don’t understand how I would adapt it. (See, for example: django queryset for the many-to-many field )

, , , , / "". , , , bird_to_see :

def find_aviaries(self):
    person_birds = set(self.birds_to_see.all())
    found_aviaries = []
    for aviary in Aviary.objects.all():
        aviary_birds = set(aviary.birds.all())
        if person_birds.issubset(aviary_birds):
            found_aviaries.append(aviary)            
    return found_aviaries

!

+4

Source: https://habr.com/ru/post/1529722/


All Articles