Here are three very simplified class I'm working with:
class User(AbstractBaseUser):
email = models.EmailField()
name = models.CharField()
is_admin = models.BooleanField()
phone_number = models.CharField()
class Accounts(models.Model):
name = models.CharField()
users = models.ManyToManyField(settings.USR_MODEL, through='Membership',
null=True, blank=True)
customer_id = models.IntegerField()
class Membership(models.Model):
user = models.ForeignKey(User)
company = models.ForeignKey(Accounts)
is_admin = models.BooleanField(default=False)
is_billing = models.BooleanField(default=False)
is_tech = models.BooleanField(default=False)
I'd like to be able to get the Users associated with an Account and filter them by the boolean attributes is_admin, is_billing, is_tech. Right now I'm doing:
microsoft = Accounts.objects.get(customer_id=1)
I can then get tech contact by doing
ms_tech = microsoft.filter(membership__is_tech=True)
This works, however, I'd like to be able to dynamically create the queries for membership__is_tech / __is_billing / __is_admin / __is_foo / __is_bar / __is_quux / etc What is the most pythonic/djangonic way of doing this?