我如何传递列表到modelmultiechoicefield



这是参与者选择表单

class EventFormAdmin(ModelForm):
name = forms.TextInput(attrs={'class':'form-control'}),
manager = forms.Select(attrs={'class':'form-select'}),     
venue = forms.Select(attrs={'class':'form-select'}),
event_date = forms.TextInput(attrs={'class':'form-control'}),
participants = forms.ModelMultipleChoiceField(

**queryset=Participant.objects.filter().values(participant_list),** #filter for participants who applied for a specific event
widget=forms.CheckboxSelectMultiple
),
description = forms.Textarea(attrs={'class':'form-control'}),
class Meta:
model = Event 
fields = ('name', 'manager', 'venue', 'event_date', 'participants', 'description')

如何放置participant_listModelMultipleChoiceField在函数中,我通过尝试传递给表单字段

的值来描述过滤器。
def update_event(request, event_id):
event = Event.objects.get(pk=event_id)
participant_list = Participant.objects.filter(
event = event_id
)
if request.user.is_superuser:
form = EventFormAdmin(request.POST or None, instance=event)
else:
form = EventForm(request.POST or None, instance=event)
if form.is_valid():
form.save()
return redirect('all_events')
return render(request, 
'events/update_event.html', {
'participant_list':participant_list,
'event': event,
'form':form
})

感谢

form.is_valid()条件前使用此代码

form.fields.get('participant_list').queryset=participant_list

相关内容

  • 没有找到相关文章

最新更新