我正在使用很多文件,并且使用了prefech_related来优化查询
我想要用户所有子公司的邮件ID。 如何实现此代码:
class AuthUser(AbstractBaseUser, PermissionsMixin):
user_code = models.CharField(max_length=10, unique=True)
user_type = models.ForeignKey(UserTypes, on_delete=models.SET_NULL, null=True)
address = models.CharField(max_length=100, null=True)
email = models.EmailField(max_length=100, null=False)
class Company(models.Model):
company_name = models.CharField(max_length=100, null=False, unique=True)
company_short_name = models.CharField(max_length=100, null=True, blank=True)
company_type = models.CharField(max_length=1, choices=company_type, default="T")
child_company = models.ManyToManyField('self', blank=True)
users = models.ManyToManyField(AuthUser, blank=True)
我尝试了
user_data = Company.objects.filter(id=1).prefetch_related('child_company', 'child_company__users')