0

我有一系列通过外键连接的模型。

通用设置应用程序

class Company(HistoryBaseModel):
    code = models.CharField(max_length=3)
    name = models.CharField(max_length=50)
    user_company = models.ManyToManyField(User, through="UserCompany", through_fields=("company", "user"))
    

    class Meta(BaseModel.Meta):
        verbose_name = "Company"
        verbose_name_plural = "Companies"
        ordering = ('code',)

    def __str__(self):
        return f'{self.name} ({self.code})'


class UserCompany(HistoryBaseModel):

    company = models.ForeignKey(Company, on_delete=models.PROTECT, verbose_name="Company", related_name="%(app_label)s_%(class)s_company")
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="User", related_name="%(app_label)s_%(class)s_user")
    

    class Meta(BaseModel.Meta):
        verbose_name = "User's Company"
        verbose_name_plural = "User's Companies"
        constraints.UniqueConstraint(fields=['company', 'user'], name='unique_user_company')
        

    def __str__(self):
        return f'{self.user} ({self.company})'


class MachineTypes(HistoryBaseModel):
    manufacturer = models.CharField(max_length=50)
    family = models.CharField(max_length=20)
    type = models.CharField(max_length=10)
    series = models.CharField(max_length=10)
    company_machine_types = models.ManyToManyField(Company, through = 'CompanyMachineTypes', through_fields=('type_series', 'company'))
    

    class Meta(BaseModel.Meta):
        verbose_name = "machine Type"
        verbose_name_plural = "machine Types"
        constraints.UniqueConstraint(fields=['family', 'type', 'series'], name='unique_type_series')
        ordering = ('manufacturer', 'type', 'series')

    def __str__(self):
        return f'{self.manufacturer} {self.type}-{self.series}'

    # Unique: Type, Series  

class CompanyMachineTypes(HistoryBaseModel):
    company = models.ForeignKey(Company, on_delete=models.PROTECT, related_name="%(app_label)s_%(class)s_company")
    type_series = models.ForeignKey(MachineTypes, on_delete=models.PROTECT, related_name="%(app_label)s_%(class)s_type_series", verbose_name='Type/Series')
    

    class Meta(BaseModel.Meta):
        verbose_name = "Company machine Types"
        verbose_name_plural = "Company machine Types"
        constraints.UniqueConstraint(fields=['company', 'machineTypes'], name='unique_company_typeSeries')

    def __str__(self):
        return f'{self.company} ({self.type_series})'
    

class Machine(HistoryBaseModel):
    type = models.ForeignKey(MachineTypes, on_delete=models.PROTECT, related_name="%(app_label)s_%(class)s_type")
    company = models.ForeignKey(Company, on_delete=models.PROTECT, related_name="%(app_label)s_%(class)s_company")
    registration = models.CharField(max_length=10, unique=True)
    sn= models.CharField(max_length=10, unique=True)
    
    

    class Meta(BaseModel.Meta):
        verbose_name = "machine"
        verbose_name_plural = "machine"
        ordering = ('company', 'type', 'sn')

    def __str__(self):
        return f'{self.company} {self.registration} ({self.sn})'

电源APP

class PowerSources(HistoryBaseModel):
    
    Machine_type = models.ForeignKey("genetics.MachineTypes", on_delete=models.PROTECT)
    code = models.CharField(max_length=10, unique=True)
    description = models.CharField(max_length=200, blank=True)
    type = models.ForeignKey("genetics.SystemVariables", verbose_name= ("Power Source Type"), on_delete=models.PROTECT, limit_choices_to={"variable_type": 2})
    current_type = models.BooleanField(default=True, choices= ((True, "AC"), (False, "DC")))
    voltage = models.DecimalField(max_digits=3, decimal_places=2)
    limit = models.DecimalField(max_digits=7, decimal_places=2)
    phases = models.PositiveSmallIntegerField(help_text="If Current Type is set as DC, this field will default to 0")

    def save(self):
        if self.type == False:
            self.phases = 0
        super().save()

class MachinePowerSources(PowerSources):

    Machine_type = None
    Machine = models.ForeignKey("genetics.Machine", verbose_name= ("Machine"), on_delete=models.PROTECT, limit_choices_to={?WHAT SHOULD I ENTER HERE?})

如何将 MachinePowerSources.Machine 中的_choices_to 限制为属于 Company.user_company 的机器类型。IE:我想将返回的机器选项限制为由公司和扩展用户配置文件的 UserCompany 模块为用户授权的机器选项。如果机器 X 属于公司 1,机器 Y 属于公司 2,并且用户在 UserCompany 中只有公司 2,则模块应该只接受/返回机器 y。

4

0 回答 0