My Profile model have two m2m fields in one model - region and country. And as you know, country has there own region with foreignkey.
I want to try counting profiles per region - not only include region but also in country__region.
i.e.) If some have only has Africa region, and other has only Congo country (with the region Africa), I want to filter them at one.
I try to solve it using annotate. I can find count of region individually like below
profiles = Profile.objects.all()
region_count = profiles.values('region').annotate(region_count=Count('region'))
country_count = profiles.values('region').annotate(region_count=Count('country__region'))
But how can I count queryset with specific region, filtering with region and region__country at once? Is there any possible method?
Here's my profile / country model. The region model just has name field.
class Profile(models.Model):
region = models.ManyToManyField(
Region,
verbose_name="Region(s) of interest",
blank=True,
)
country = models.ManyToManyField(
Country,
related_name="country",
verbose_name="Countries of interest",
blank=True,
)
...
class Country(models.Model):
region = models.ForeignKey(
Region,
null=True,
blank=True,
)
...
Thanks for any help.
Summary
I want to count queryset with region and country__region at once with annotate.
region_count = profiles.values('region').annotate(Count('region'), Count('country__region')).distinct()regionandcountry__region. My problem detail - I want to get all profiles includingregionorcountry__regionper region. i.e.)africa- profiles having africaregionand having africa incountry__regionboth.