3 回答

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
經(jīng)過長(zhǎng)時(shí)間的尋找。我通過用表格解決它來欺騙它
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貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超22個(gè)贊
我想我了解了您的問題,您想以簡(jiǎn)單的方式連接表格,對(duì)吧?,我建議看看這個(gè)和這個(gè)
并以這種方式修復(fù)這些模型關(guān)系,因?yàn)榈綍r(shí)候這將是一個(gè)問題:
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 (因?yàn)槟惚仨毾葘⑺鼘?dǎo)入到你的代碼中):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)
因?yàn)槿绻闶?django 的新手,遲早會(huì)出現(xiàn)遷移問題

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊
我有更好的答案,而不是用形式來欺騙他們。在 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'})
添加回答
舉報(bào)