0

如果已經在另一個問題中涉及到此問題,我表示歉意 - 我是Django的新手,可能一直盯着此問題以及有關Stack的許多其他問題,現在有點太長了..在ModelForm字段中過濾模型實例

使用Django 1.8.2,我有一個模型,它顯示了一個用戶列表,我想從中排除當前用戶(即你看不到你自己)。我試圖通過覆蓋init方法來從「to_user」列表中排除當前用戶。我得到下面的錯誤(「邀請沒有to_user」),我不明白。不應該從模型中的to_user ForeignKey字段填充字段?

您的幫助和時間都非常讚賞 -

特定的錯誤是:

enter image description here

模式是:

class Invitation(models.Model): 
    from_user = models.ForeignKey(User, related_name="invitations_sent") 
    to_user = models.ForeignKey(User, related_name="invitations_received", verbose_name="User to Invite", help_text="This is Help Text") # Verbose name is the form label for Foreigh Key vars 
    message = models.CharField("Optional Message", max_length=300, blank=True) # Can add string for lavel, Setting blank=True means the field won't be required with ModelForm 
    timestamp = models.DateTimeField(auto_now_add=True) 

窗體類是:

class InvitationForm(ModelForm): 
    class Meta: # Class Meta sets the model class that the form needs to be auto-created for 
     model = Invitation 
     exclude = ['from_user'] 

    def __init__(self, user, **kwargs): 
     super(InvitationForm, self).__init__(user, **kwargs) 
     self.fields['to_user'].queryset = Invitation.objects.exclude(user=self.instance.to_user) 

觀點是:

@login_required 
def new_invitation(request): 
    if request.method == "POST": 
     invitation = Invitation(from_user=request.user) 
     form = InvitationForm(data=request.POST, instance=invitation) 
     if form.is_valid(): 
      form.save() 
     return redirect('user_home') 
    else: 
     form = InvitationForm(user=request.user) 
    return render(request, "tictactoe/new_invitation.html", {'form' : form}) 
+0

異常提到'Invitation'對象沒有'to_user'設置(該字段爲空) – karthikr

+0

感謝您的幫助,karthikr。 –

回答

1

爲什麼你已經改變了__init__方法的簽名目前尚不清楚。您不應該將用戶傳遞給super()調用。由於您沒有在任何地方使用user,並且您在實例化表單時沒有通過它,我會將其刪除。

其次,您可能希望排除instance.from_user而不是instance.to_user

class InvitationForm(ModelForm): 
    def __init__(self, *args, **kwargs): 
     super(InvitationForm, self).__init__(*args, **kwargs) 
     self.fields['to_user'].queryset = Invitation.objects.exclude(user=self.instance.from_user) 
+0

謝謝你的幫助,阿拉斯代爾。 –

+0

很高興你知道了:) – Alasdair

1

我的問題是試圖從邀請模型構建一個查詢集,它不包含我想要過濾的內容。考慮到Alasdair的建議,關鍵是將用戶模型導入到我的forms.py文件中,然後在用戶模型上構建查詢集,並從視圖中傳入登錄用戶。

的ModelForm現在看起來像:

class InvitationForm(ModelForm): 

    def __init__(self, *args, **kwargs): 
     user = kwargs.pop('user') 
     super(InvitationForm, self).__init__(*args, **kwargs) 
     self.fields['to_user'].queryset = User.objects.exclude(id=user.id) 

    class Meta: 
     model = Invitation 
     exclude = ['from_user'] 

實例化從查看

@login_required 
def new_invitation(request): 
    if request.method == "POST": 
     invitation = Invitation(from_user=request.user) 
     form = InvitationForm(user=request.user, data=request.POST, instance=invitation) 
     if form.is_valid(): 
      form.save() 
      return redirect('user_home') 
    else: 
     form = InvitationForm(user=request.user) 
    return render(request, "tictactoe/new_invitation.html", {'form' : form}) 

謝謝大家,你們的幫助。

相關問題