猿问

Django 过滤外键项

我制作简单的日常清洁检查应用程序。该应用程序很简单,用户观察某些区域并检查这些区域是否干净。经过所有检查后,用户需要向批准其日常清洁工作的当局报告。这里的权威机构是创建者、检查者和签名者。这里的 cln_daily 模型代表日常清洁工作,将有来自用户模型的制造者、检查者和签名者。


我有一个名为user的应用程序,它在model.py中对用户模型进行了定制


class User(AbstractUser):

    username = models.CharField(max_length=50, unique=True)

    email = models.EmailField(_('email address'), unique=True)

    phone = models.IntegerField(_('phone number'), unique=True, blank=True, null=True)

该用户与组查询集具有多对多关系:创建者、检查者、签名者


>>> print(Group.objects.all()) 

<QuerySet [<Group: maker>, <Group: checker>, <Group: signer>]>

在另一个名为clean的应用程序中,我有cln_daily模型,该模型与基于其组的用户模型具有外键关系。


class cln_daily(models.Model):

   .

   .

   user_maker = models.ForeignKey(User,verbose_name="Maker's Signature",on_delete=models.CASCADE, related_name="makerSignature", blank=True, null=True,)

   user_checker = models.ForeignKey(User, verbose_name="Checker's Signature",on_delete=models.CASCADE, related_name="checkerSignature", blank=True, null=True)

   user_signer = models.ForeignKey(User, verbose_name="Signer's Signature",on_delete=models.CASCADE, related_name="signerSignature", blank=True, null=True)


所以我的意思是user_maker查询集具有仅属于组maker 的用户。user_checker具有仅属于组checker 的用户,而user_signer查询集具有仅属于组signer 的用户。如何在该model.py中表达过滤后的用户模型?


料青山看我应如是
浏览 140回答 3
3回答

繁星淼淼

经过长时间的寻找。我通过用表格解决它来欺骗它class MCSForm(forms.ModelForm):&nbsp; &nbsp; class Meta:&nbsp; &nbsp; &nbsp; &nbsp; model = cln_daily&nbsp; &nbsp; &nbsp; &nbsp; fields = ("__all__")&nbsp; &nbsp; def __init__(self, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__(*args, **kwargs)&nbsp; &nbsp; &nbsp; &nbsp; self.fields['user_maker'].queryset = User.objects.filter(groups__name='maker')&nbsp; &nbsp; &nbsp; &nbsp; self.fields['user_checker'].queryset = User.objects.filter(groups__name='checker')&nbsp; &nbsp; &nbsp; &nbsp; self.fields['user_signer'].queryset = User.objects.filter(groups__name='signer')

茅侃侃

我想我了解了您的问题,您想以简单的方式连接表格,对吧?,我建议看看这个和这个并以这种方式修复这些模型关系,因为到时候这将是一个问题:class cln_daily(models.Model):&nbsp; &nbsp;.&nbsp; &nbsp;.&nbsp; &nbsp;user_maker = models.ForeignKey(User,verbose_name="Maker's Signature",on_delete=models.CASCADE, related_name="makerSignature", blank=True, null=True,)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^^^^&nbsp; &nbsp;user_checker = models.ForeignKey(User, verbose_name="Checker's Signature",on_delete=models.CASCADE, related_name="checkerSignature", blank=True, null=True)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^^&nbsp; &nbsp;user_signer = models.ForeignKey(User, verbose_name="Signer's Signature",on_delete=models.CASCADE, related_name="signerSignature", blank=True, null=True)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^最好不要使用in User,或者使用此模式作为 str (因为你必须先将它导入到你的代码中):ForeignKeyFieldget_user_model()class cln_daily(models.Model):&nbsp; &nbsp;.&nbsp; &nbsp;.&nbsp; &nbsp;user_maker = models.ForeignKey('myapp.User',verbose_name="Maker's Signature",on_delete=models.CASCADE, related_name="makerSignature", blank=True, null=True,)&nbsp; &nbsp;user_checker = models.ForeignKey('myapp.User', verbose_name="Checker's Signature",on_delete=models.CASCADE, related_name="checkerSignature", blank=True, null=True)&nbsp; &nbsp;user_signer = models.ForeignKey('myapp.User', verbose_name="Signer's Signature",on_delete=models.CASCADE, related_name="signerSignature", blank=True, null=True)因为如果你是 django 的新手,迟早会出现迁移问题

尚方宝剑之说

我有更好的答案,而不是用形式来欺骗他们。在 FKField 中使用limit_choices_toUser = get_user_model()class cln_daily(models.Model):&nbsp; &nbsp;.&nbsp; &nbsp;.&nbsp; &nbsp;user_maker = models.ForeignKey(User,verbose_name="Maker's Signature",on_delete=models.CASCADE, related_name="makerSignature", blank=True, null=True,limit_choices_to={'groups__name':'maker'})&nbsp; &nbsp;user_checker = models.ForeignKey(User, verbose_name="Checker's Signature",on_delete=models.CASCADE, related_name="checkerSignature", blank=True, null=True,limit_choices_to={'groups__name':'checker'})&nbsp; &nbsp;user_signer = models.ForeignKey(User, verbose_name="Signer's Signature",on_delete=models.CASCADE, related_name="signerSignature", blank=True, null=True,limit_choices_to={'groups__name':'signer'})
随时随地看视频慕课网APP

相关分类

Python
我要回答