class AddPerformerForm(ModelForm):
categories = forms.ModelChoiceField(queryset=Category.objects.all(),
widget=forms.CheckboxSelectMultiple,
empty_label=None)
user = forms.CharField(required=False)
class Meta:
model = Performer
fields = ['user', 'categories']
def create_performer(request):
if request.method == 'POST':
form = AddPerformerForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
instance.user = request.user
instance.save()
return HttpResponseRedirect('/')
else:
return HttpResponse(request.POST)
else:
form = AddPerformerForm(request.POST or None)
context = {"form": form}
return render(request, "performerslist/addperformer.html", context)
class AddPerformerForm(ModelForm):
categories = forms.ModelChoiceField(queryset=Category.objects.all(),
widget=forms.CheckboxSelectMultiple,
empty_label=None)
class Meta:
model = Performer
exclude = ['user']
fields = ['categories']
def create_performer(request):
form = AddPerformerForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
instance = form.save(commit=False)
instance.user = request.user
instance.save()
return HttpResponseRedirect('/')
else:
return HttpResponse('no')
context = {"form": form}
return render(request, "performerslist/addperformer.html", context)