3 回答
TA贡献1775条经验 获得超11个赞
经过长时间的寻找。我通过用表格解决它来欺骗它
class MCSForm(forms.ModelForm):
class Meta:
model = cln_daily
fields = ("__all__")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['user_maker'].queryset = User.objects.filter(groups__name='maker')
self.fields['user_checker'].queryset = User.objects.filter(groups__name='checker')
self.fields['user_signer'].queryset = User.objects.filter(groups__name='signer')
TA贡献1842条经验 获得超21个赞
我想我了解了您的问题,您想以简单的方式连接表格,对吧?,我建议看看这个和这个
并以这种方式修复这些模型关系,因为到时候这将是一个问题:
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)
^^^^
最好不要使用in User,或者使用此模式作为 str (因为你必须先将它导入到你的代码中):ForeignKeyFieldget_user_model()
class cln_daily(models.Model):
.
.
user_maker = models.ForeignKey('myapp.User',verbose_name="Maker's Signature",on_delete=models.CASCADE, related_name="makerSignature", blank=True, null=True,)
user_checker = models.ForeignKey('myapp.User', verbose_name="Checker's Signature",on_delete=models.CASCADE, related_name="checkerSignature", blank=True, null=True)
user_signer = models.ForeignKey('myapp.User', verbose_name="Signer's Signature",on_delete=models.CASCADE, related_name="signerSignature", blank=True, null=True)
因为如果你是 django 的新手,迟早会出现迁移问题
TA贡献1788条经验 获得超4个赞
我有更好的答案,而不是用形式来欺骗他们。在 FKField 中使用limit_choices_to
User = get_user_model()
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,limit_choices_to={'groups__name':'maker'})
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'})
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'})
添加回答
举报