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?
EDIT:
And what if I have another model called Country? Here is the model:
class Country(models.Model):
name = models.CharField(max_length=255)
wine_rg = models.ManyToManyField(WineRegion, blank=True)
def __str__(self):
return self.name
I want to access all vineyards from the wine region, but now I have a list of Country IDs. How can I solve this?
I can do something like this:
wine_region_id = Country.objects.filter(id__in=country).distinct().values('wine_rg')
vineyardList = vineyardList.filter(wineregion__in=wine_region_id)
But I'm curious is there any better method to do something like this?