【问题标题】:Limit Choices to Foreign Key based on lookups that span relationships基于跨越关系的查找将选择限制为外键
【发布时间】:2015-12-01 00:41:45
【问题描述】:

这是我目前所拥有的。

models.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"))

非常感谢任何帮助。 提前致谢。

【问题讨论】:

    标签: python django django-models foreign-keys


    【解决方案1】:

    无法使用limit_choices_to 在数据库级别动态定义它。您只需在表单/视图等上定义它。即使用modelchoicefield

    【讨论】:

      猜你喜欢
      • 2011-09-18
      • 2012-10-10
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      相关资源
      最近更新 更多