Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 718143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:29:35+00:00 2026-05-14T05:29:35+00:00

I am having difficulties with forms, specifically ModelMultipleChoiceField. I’ve pieced together this code from

  • 0

I am having difficulties with forms, specifically ModelMultipleChoiceField.

I’ve pieced together this code from various examples, but it sadly doesn’t work.

I would like to be able to:

  1. Search for some Works on work_search.html
  2. Display the results of the search, with checkboxes next to each result
  3. Select the Works I want, via the checkboxes
  4. After pressing Add, display which works were selected.

I believe everything is okay except the last part. The page simply displays “works” 🙁

Here is the code – sorry about the length.

Models.py

class Work(models.Model):
    title = models.CharField(max_length=200)
    artist = models.CharField(max_length=200)
    writers = models.CharField(max_length=200)

    def __unicode__(self):
        return self.title + ' - ' + self.artist

forms.py

class WorkSelectForm(forms.Form):
    def __init__(self, queryset, *args, **kwargs):
        super(WorkSelectForm, self).__init__(*args, **kwargs)
        self.fields['works'] = forms.ModelMultipleChoiceField(queryset=queryset,       widget=forms.CheckboxSelectMultiple())

views.py

def work_search(request):
    query = request.GET.get('q', '')
    if query:
        qset = (
            Q(title__icontains=query) |
            Q(artist__icontains=query) |
            Q(writers__icontains=query)
        )
        results = Work.objects.filter(qset).distinct()
        form = WorkSelectForm(results)
        return render_to_response("work_search.html", {"form": form, "query": query })    
    else:
        results = []
    return render_to_response("work_search.html", {"query": query })

def add_works(request):
    #if request.method == POST:
    form = WorkSelectForm(request.POST)
    #if form.isvalid():
    items = form.fields['works'].queryset
    return render_to_response("add_works.html", {"items":items})

work_search.html

{% extends "base.html" %}
{% block content %}
  <h1>Search</h1>
  <form action="." method="GET">
    <label for="q">Search: </label>
    <input type="text" name="q" value="{{ query|escape }}">
    <input type="submit" value="Search">
  </form>

  {% if query %}
    <h2>Results for "{{ query|escape }}":</h2>
    <form action="add_works" method="post">
        <ul>
        {% if form %}
            {{ form.as_ul }}
        {% endif %}
        </ul>
        <input type="submit" value="Add">
    </form>
  {% endif %}
{% endblock %}

add_works.html

{% extends "base.html" %}
{% block content %}
    {% if items %}
        {% for item in items %}
            {{ item }}
        {% endfor %}
    {% else %}
        <p>Nothing selected</p>
    {% endif %}
{% endblock %}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:29:36+00:00Added an answer on May 14, 2026 at 5:29 am

    In add_works, you’re not constructing your WorkSelectForm the right way. It’s expecting as a first parameter the queryset of possible/authorized choices, then the POST data.

    Also, you’re not accessing the selected works correctly from the form. You have to use is_valid method on the form, then use cleaned_data as described in the doc.

    From what I see in your work_search view, there’s no restriction on which Work objects you can search then add to the result, so you could do simply:

    def add_works(request):
        #if request.method == POST:
        form = WorkSelectForm(Work.objects.all(), request.POST)
        if form.is_valid():
            # the items are in form.cleaned_data['works']
            items = form.cleaned_data['works']
            return render_to_response("add_works.html", {"items":items})
        else:
           # handle error case here
           ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It gets recorded as an instance of django.contrib.auth.models.User. May 14, 2026 at 10:27 pm
  • Editorial Team
    Editorial Team added an answer So bug was at another place... May 14, 2026 at 10:27 pm
  • Editorial Team
    Editorial Team added an answer Check out getScaledHeight() and getScaledWidth() in EncodedImage. It's a dirty… May 14, 2026 at 10:27 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.