Home > Net >  Access many to many field from queryset Django
Access many to many field from queryset Django

Time:05-25

I have two models:

class Vineyard(models.Model):
    name = models.CharField(max_length=255, blank=True)

    def __str__(self):
        return self.name


class WineRegion(models.Model):
    name = models.CharField(max_length=255)
    vineyards = models.ManyToManyField(Vineyard, blank=True)

    def __str__(self):
        return self.name

And I want to access all vineyards from the wine region. Here is what I've tried:

if len(id_list) > 0:
    wr = WineRegion.objects.filter(id__in=id_list)
    vineyardList = wr.vineyards.all()

But it gives me an error ---> AttributeError: 'QuerySet' object has no attribute 'vineyards'

How can I solve this?

CodePudding user response:

Since wr is a list of WineRegions, you need to iterate through them first:

vineyardList = []
for wr in WineRegion.objects.filter(id__in=id_list):
    for vineyard in wr.vineyards.all():
        vineyardList.append(vineyard)

But this will hit your db multiple times just to get each vineyard objects. Instead, you can access the related name wineregion from Vineyard to get all the vineyard objects in one query like this:

vineyardList = Vineyard.objects.filter(wineregion__in=WineRegion.objects.filter(id__in=id_list))

or simply:

vineyardList = Vineyard.objects.filter(wineregion__in=id_list)
  • Related