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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:15:15+00:00 2026-05-24T06:15:15+00:00

I have the following Django models: class Contact(models.Model): id #primary key #Other contact info

  • 0

I have the following Django models:

class Contact(models.Model):
    id #primary key
    #Other contact info

class ContactType(models.Model):
    contacttype_choices=(('Primary', 'Primary'),
                         ('Billing', 'Billing'),
                         ('Business', 'Business'),
                         ('Technology', 'Technology'))
    contact=models.ForeignKey(Contact)
    type=models.CharField(choices=contacttype_choices, max_length=30)
    class Meta:
        unique_together=('contact', 'type')

So any contact object can have up to four contact types, with each type either being present or absent. I would like to make a Model Form for Contact with a multiple choice field for contact type. How can I populate this contact type field with the existing values when I construct the Contact form with a Contact instance?

Edit: To clarify, I want one checkbox to be created for each of those four choices, and if the form is instantiated with a model instance, then I want the checkboxes to be checked for each of the related objects that exists, similar to what happens automatically with the rest of the fields.

  • 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-24T06:15:18+00:00Added an answer on May 24, 2026 at 6:15 am

    I would probably structure the models as such, so I can choose which contact type when creating/editing the Contact. If ContactType is related as a ManyToMany, we automatically get a ModelMultpleChoiceField in the ModelForm, and all we need to do is use the CheckboxSelectMultiple widget to get the output you’re looking for.

    When we pass an instance of Contact to the ContactForm, Django will bind any pre-selected ContactType choices and check the checkboxes for us.

    Setting the title of each ContactType to unique does the same as unique_together on the contact and contact_type.

    #models.py
    class Contact(models.Model):
        #other fields
        contact_types = models.ManyToMany(ContactType)
    
    class ContactType(models.Model):
        title = models.CharField(max_length=20, unique=true)
    
    #forms.py
    class ContactForm(forms.ModelForm):
        class Meta:
            model = Contact
    
        def __init__(self, *args, **kwargs):
            super(ContactForm, self).__init__(*args, **kwargs)
            self.fields['contact_types'].widget = forms.CheckboxSelectMultiple()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following Django model: class Info(models.Model): instrument = models.ForeignKey('Instrument') date
I have the following abstract Django models: class Food(models.Model): name = models.CharField(max_length=100) class Meta:
I have following models setup in my Django application class School(models.Model): name = models.TextField()
Let's assume we have following models: from django.db import models class Foo(models.Model): name =
I have the following Django models: - class Company(models.Model): name = models.CharField(max_length=50) is_active =
Let's say I have the following Django models: class A(models.Model): keywords = models.ManyToManyField(Keyword) class
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have the following model in Django: from django.db import models class User(models.Model): pen_name
I have the following Django model: class opetest(models.Model): name = models.CharField(max_length=200) people = models.ManyToManyField(User,
I have the following django model (mapped to table 'A'): class A(models.Model): name =

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.