基于跨关系的查找将选择限制为外键

2024-04-25 15:06:24 发布

您现在位置:Python中文网/ 问答频道 /正文

这就是我目前所拥有的

型号.py

class Company(models.Model):
    company_name = models.CharField('Company', max_length=40, unique=True)
    company_type = models.CharField('Type', max_length=20, null=True, blank=True)

class MyUser(AbstractBaseUser):
    email = models.EmailField(max_length=80, unique=True)
    first_name = models.CharField('First Name', max_length=25)
    last_name = models.CharField('Last Name', max_length=25)
    company = models.ForeignKey(Company,limit_choices_to *********

class Organization(models.Model):
    owner = models.ForeignKey(Company, null=True, related_name='owner')
    client = models.ForeignKey(Company, null=True, related_name='client')

我希望当前用户(登录的用户)为他/她的公司的客户创建用户

所以,我猜是

MyCompany = request.user.company
company = models.ForeignKey(Company, limit_choices_to=(Organization__owner="MyCompany"))

非常感谢您的帮助。 提前谢谢


Tags: 用户nametruemodelmodelsnulllengthcompany