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 6966305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:10:28+00:00 2026-05-27T16:10:28+00:00

I have model with ManyToManyField. Now I need form but don’t need select field

  • 0

I have model with ManyToManyField. Now I need form but don’t need select field in template.

class Foo(models.Model):
    name = models.CharField(max_length=50)
    short_description = models.CharField(max_length=100)
    price = models.IntegerField()

    def __unicode__(self):
        return self.name

class Bar(models.Model):
    foo = models.ManyToManyField(Foo, blank=True, null=True, related_name='foos')

    def __unicode__(self):
        return unicode(self.id)

What I really need is to display all data from the Foo model with checkboxes in template, instead of select field which I have if use model.Form and {{ form }} call in template.

class BarForm(forms.ModelForm):
    class Meta:
        model = Bar

view.py

def show_form(request, id):
    foo = get_object_or_404(Foo, id=id)
    form = BarForm()
    ...
  • 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-27T16:10:29+00:00Added an answer on May 27, 2026 at 4:10 pm

    To show a ManyToManyField as checkboxes instead of a select field, you need to set the widget in the Meta class of the appropriate ModelForm subclass. Then to show a custom label on each checkbox, create your own form field class derived from ModelMultipleChoiceField, and override label_from_instance. You may also add HTML tags to the string returned by label_from_instance to make it look as pretty as you want, but remember to wrap the returned string with mark_safe.

    from django.forms.widgets import CheckboxSelectMultiple
    from django.forms.models import ModelMultipleChoiceField
    ...
    class CustomSelectMultiple(ModelMultipleChoiceField):
        def label_from_instance(self, obj):
            return "%s: %s %s" %(obj.name, obj.short_description, obj.price)
    
    class BarForm(forms.ModelForm):
        foo = CustomSelectMultiple(queryset=Foo.objects.all())
        class Meta:
            model = Bar
            widgets = {"foo":CheckboxSelectMultiple(),}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have user profile model with M2M field class Account(models.Model): ... friends = models.ManyToManyField('self',
I have the following models: class ProjectUser(models.Model): categories = models.ManyToManyField('UserCategory', blank=True, null=True) user_id =
I have to models: class A(Model): b = models.ManyToManyField(B) class B(Model): # fields What
I have the following model: class Task(Model): solvers = ManyToManyField(User, related_name='slvrs') Now I want
I have the following Model: class Group(models.Model): member = models.ManyToManyField(Player, through='GroupMember') name = models.CharField(max_length=20,
I have the models: class Article(models.Model): title = models.TextField(blank=True) keywords = models.ManyToManyField(Keyword, null=True, blank=True)
I have model Foo which has field bar. The bar field should be unique,
I have model public class UploadOptionModel { public UploadOptionModel() { OutputFormat = outputFormatList.Select(i =>
I have model: # encoding: utf-8 class Tag include Mongoid::Document field :name, type: String
Suppose I have this model (not real code): class Message(models.Model): content = models.CharField(...) mentions

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.