2

時,「選擇一個有效的選擇。 不是可用的選項之一」的錯誤我想一個ManyToManyField的選擇限制爲那些匹配一個ForeignKey。表單顯示正確,但在保存結果時出現錯誤「選擇一個有效的選擇不是可用的選項之一」。 提交多對多的ModelForm

之前我試圖通過在視圖將參數傳遞給形式,並且然後使用該參數來過濾的查詢集來限制查詢集。

型號:

class VenueEventTimeslot(models.Model): 
    venue = models.ForeignKey(Venue) 
    name = models.CharField(max_length=255) 

class VenueEvent(models.Model): 
    venue = models.ForeignKey(Venue) 
    event_timeslots = models.ManyToManyField(VenueEventTimeslot)  

class VenueEventForm(ModelForm): 
    event_timeslots = ModelMultipleChoiceField(queryset=None, widget=CheckboxSelectMultiple()) 

    def __init__(self, *args, **kwargs): # limit timeslots to those of the venue only 
     venue_obj = kwargs.pop('venue_obj',None) 
     super(VenueEventForm, self).__init__(*args,**kwargs) 
     self.fields['event_timeslots'].queryset=VenueEventTimeslot.objects.filter(venue=venue_obj) 

    class Meta: 
     model = VenueEvent 
     fields = ['event_timeslots'] 

瀏覽:

@login_required 
def calendar(request, pk): 
    venue = Venue.objects.get(pk = pk) 

    if request.method == "POST": 
     form = VenueEventForm(request.POST) 
     if form.is_valid(): 
      # form stuff 
     else: 
      form = VenueEventForm(venue_obj = venue) 

    context = {'venue':venue, 'form':form} 
    return render(request, ... , context) 

但是,如果我從視圖通過查詢集,它完美的作品。

型號:

class VenueEventTimeslot(models.Model): 
    # same as above 

class VenueEvent(models.Model): 
    # same as above 

class VenueEventForm(ModelForm): 
    class Meta: 
    model = VenueEvent 
    fields = ['date','client_name','event_timeslots'] 
    widgets = { 
     'date': SelectDateWidget(), 
     'event_timeslots': CheckboxSelectMultiple(), 
    } 

瀏覽:

@login_required 
def calendar(request, pk): 
    venue = Venue.objects.get(pk = pk) 

    if request.method == "POST": 
     form = VenueEventForm(request.POST) 
     if form.is_valid(): 
      # form stuff 
     else: 
      form = VenueEventForm() 
    form.fields['event_timeslots'].queryset=VenueEventTimeslot.objects.filter(venue=venue) 

    context = {'venue':venue, 'form':form} 
    return render(request, ..., context) 

會有人能夠擺脫這方面的一些輕?

+0

所以你說你的模型形式,__init __()不能正常工作? – 2015-08-14 19:22:16

+0

提出了一個答案,讓我知道這是如何適用於你。 – 2015-08-14 19:45:36

回答

1

我剛剛解決與此類似昨天是在這裏,How To Exclude A Value In A ModelMultipleChoiceField?一個問題,但我認爲你的初始化函數的問題是,它被格式化的方式。相反場地= venue_obj的,你需要改變它的PK = venue_obj,因爲它出現在你越來越場地PK的看法,而不是VenueEvent會場屬性,我重新格式化您的形式有點使它看起來更乾淨。

forms.py

class VenueEventForm(ModelForm): 

    def __init__(self, *args, **kwargs): # limit timeslots to those of the venue only 
     venue_obj = kwargs.pop('venue_obj') 
     super(VenueEventForm, self).__init__(*args,**kwargs) 
     self.fields['event_timeslots'] = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple(), queryset=VenueEventTimeslot.objects.filter(pk=venue_obj)) 

    class Meta: 
     model = VenueEvent 
     fields = ['event_timeslots'] 

views.py

@login_required 
def calendar(request, pk): 
    venue = Venue.objects.get(pk = pk) 
    if request.method == "POST": 
     form = VenueEventForm(request.POST, venue_obj=venue) 
     if form.is_valid(): 
      # form stuff 
     else: 
      print VenueEventForm.errors 

    else: 
     form = VenueEventForm(venue_obj=venue) 
     context = {'venue':venue, 'form':form} 
     return render(request, ... , context) 
+0

對不起,你在哪裏看到場地=場地? – wasabigeek

+0

我看到你現在在哪裏,我錯誤地看了看,讓我看看我是否可以在這裏編輯我的答案。 – 2015-08-14 19:58:21

+0

你究竟想要在__inti__函數中彈出什麼,你試圖從列表中完全刪除。 – 2015-08-14 20:07:17