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

  • Home
  • SEARCH
  • 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 1018541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:52:23+00:00 2026-05-16T10:52:23+00:00

Assuming that I have such model COLORS= ( (‘R’, ‘Red’), (‘B’, ‘Yellow’), (‘G’, ‘White’),

  • 0

Assuming that I have such model

COLORS= (
    ('R', 'Red'),
    ('B', 'Yellow'),
    ('G', 'White'),
)
class Car(models.Model):
    name = models.CharField(max_length=20)
    color= models.CharField(max_length=1, choices=COLORS)

It displays as a selectbox in the admin panel however I would like my admin-user to multi select those colors like many-to-many relationship, how can this be achieved without a ('RB', 'Red&Blue'), type of logic

  • 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-16T10:52:24+00:00Added an answer on May 16, 2026 at 10:52 am

    Can a Car have multiple colors? In that case color ought to be a many to many relationship rather than a CharField. If on the other hand you want to do something like Unix permissions (i.e. Red + Blue, Red + Blue + Green etc.) then assign numeric values of to each of them and make color an integer field.

    Update

    (After reading comment) You can use a custom form to edit your model in Admin instead of the default ModelForm. This custom form can use a multiple choice widget that lets users select multiple colors. You can then override the clean() method of the form to return a suitably concatenated value (‘RB’ etc.).

    Update 2

    Here is some code:

    First, remove the choices from the model field. Also increase its maximum size to 2. We don’t want choices here – if we do, then we’ll have to add a choice for each combination of colors.

    class Car(models.Model):
        ...
        color= models.CharField(max_length=2)
    

    Second add a custom ModelForm to use in admin app. This form will override color and instead declare it as a multiple choice field. We do need choices here.

    COLORS= (
        ('R', 'Red'),
        ('B', 'Yellow'),
        ('G', 'White'),
    )
    
    class CarAdminForm(ModelForm):
        color = forms.MultipleChoiceField(choices = COLORS)
    
        class Meta:
            model = Car
    
        def clean_color(self):
            color = self.cleaned_data['color']
            if not color:
                raise forms.ValidationError("...")
    
            if len(color) > 2:
                raise forms.ValidationError("...")
    
            color = ''.join(color)
            return color
    

    Note that I have added only a couple of validations. You may want more and/or customize the validations.

    Finally, register this form with admin. Inside your admin.py:

    class CarAdmin(admin.ModelAdmin):
        form = CarAdminForm
    
    admin.site.register(Car, CarAdmin)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a hard time phrasing this question in such a way that doesn't
I have a need, for a python script I'm creating, to first get just
I'm attempting to develop an application that scapes html of a site for relevant
I am writing a numerical program that should be somewhat fast, and is also
I'm attempting to programmatically chain asynchronous operations in C#4, such as Writes to a
I've been writing tests for a while now and I'm starting to get the
I'm building static libraries (right now libpng ) in Microsoft Visual Studio 2008 SP1.
I am using Eclipse with GAE on a MacBook Pro with GChart. My problem
In an Excel sheet I will receive the data, which in turn I need

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.