I’m trying to do something which I would think is quite common, but I’m just really unsure how to tackle this.
What I currently do: Currently I produce a list of check boxes (using ‘CheckboxSelectMultiple’) on my form which auto ticks all the users who are in ‘members’ from the list of all Users in django (produced by “queryset=User.objects.all()”).
What I want to actually do: I don’t want to list every user in Django, I just want to show a list of Users in ‘members’.
How I think it can be done: I think I can do this by modifying the queryset to be something like “project.members.all()” where project = Project (a specific instance of project). But how do I pass this context to my form?
# models.py
class Project(models.Model):
name = models.CharField(max_length=100)
members = models.ManyToManyField(User, related_name="members", blank=True, null=True)
# forms.py
class ProjectSettings(forms.Form):
summary = forms.CharField(max_length=200)
members = forms.ModelMultipleChoiceField(queryset=User.objects.all(), widget=forms.CheckboxSelectMultiple())
#template snippet
<form method="post" action="/projects/{{ project.slug }}/settings/save/">
{% for field in form %}
<div class="form-row">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
{% if field.help_text %}
<p class="help-text">{{ field.help_text }}</p>
{% endif %}
</div>
{% endfor %}
# view.py
def update_project(request, project_slug):
if request.method == 'POST':
form = ProjectSettings(request.POST)
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
project.summary = form.cleaned_data['summary']
project.members = form.cleaned_data['members']
project.save()
return HttpResponseRedirect('/projects/' + project.slug + '/')
else:
data_dict = {'summary': project.summary, 'members': project.members.all()}
form = ProjectSettings(initial=data_dict)
return render_to_response('update_project.html', {'form': form}, context_instance=RequestContext(request))
I hope this makes sense, and I do apologise as I really think this is something simple I’m missing here – but I haven’t been able to find a specific example showing how to do this with non-model forms.
Thanks in advance,
Jamie
I’m guessing that you are leaving some stuff out of your Project model. Your view is also calling
project.members.all()andproject.summarywith out getting a project in the else statement.Assuming that you have a summary field in the projects model, if you want to use a form and not a model form, then:
forms.py
and in your views.py you would pass in the qs to the form:
Using a ModelForm makes sense here if you do indeed have your summary field in your Project model.
forms.py:
views.py: