2

I wanted to show multiple choices to admin so that at one time he can select more then one from these choices. I can do this using check boxes fields.I have tried this but instead of showing check boxes it shows me drop down list of choices.

Here is my code.

models.py

class segmentation_Rules(models.Model):
        Segmentation_Rules_CHOICES = (
                        (1, 'At least one order'),
                        (2, 'Have reward points'),
                        )
        Rules       =models.CharField(max_length=100, blank=True,verbose_name="Select rules for customer segmentation",choices=Segmentation_Rules_CHOICES) 

forms.py

class Segmentation_Form(ModelForm):
        Rules = forms.MultipleChoiceField( widget=forms.CheckboxSelectMultiple)

admin.py

class MyAdmin(admin.ModelAdmin):
    form = Segmentation_Form

So please show me some way so that admin can select multiple fields from choices.

EDIT:

And if I remove the choices from models and define them into forms then there is just a text field shown to admin with no choices.

Segmentation_Rules_CHOICES = (
            (1, 'At least one order'),
            (2, 'Have reward points'),
            )

class Segmentation_Form(ModelForm):
        Rules = forms.MultipleChoiceField(choices=Segmentation_Rules_CHOICES, widget=forms.CheckboxSelectMultiple())

        class Meta:
            model=segmentation_Rules
1

4 Answers 4

2

You need to remove the choices argument from the model field definition in models.py and add choices field to the Rules form field in forms.py. Like so:

models.py

class segmentation_Rules(models.Model):
    Segmentation_Rules_CHOICES = (
        (1, 'At least one order'),
        (2, 'Have reward points'),
    )
    Rules = models.CharField(max_length=100, blank=True, verbose_name="Select rules for customer segmentation") 

forms.py

class Segmentation_Form(ModelForm):
    Rules = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(), choices=models.segmentation_Rules.Segmentation_Rules_CHOICES)
1
  • 1
    I do so, selected choices are being stored correctly but the problem is when admin wants to edit a record in django-admin selected choices are not being shown. Dec 30, 2020 at 10:16
1

I am using this and its working fine

Rules = forms.MultipleChoiceField(choices=mychoices, widget=forms.CheckboxSelectMultiple)

i think you don't need () at the send of CheckboxSelectMultiple

3
  • it is not giving any error but displaying choices in a drop down list instead of check boxes to me .
    – Inforian
    Jan 30, 2013 at 9:23
  • That exact same code worked for me in the frontend form. I am not sure if something is being overridden in Admin side by Django. i will have a look
    – Mirage
    Jan 30, 2013 at 9:48
  • Thanks,I want this for django admin , so if you found something please posted here
    – Inforian
    Jan 30, 2013 at 9:55
1

Use Multiple Choices in Admin with CharField Model

It stores the choices seperated by comma.

models.py and admin.py as they are

forms.py

from my_project.model import segmentation_Rules 

class Segmentation_Form(ModelForm):
      Rules = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=segmentation_Rules.Segmentation_Rules_CHOICES, required=False)

      def __init__(self, *args, **kwargs):
          super(Segmentation_Form, self).__init__(*args, **kwargs)
          if kwargs.get('instance'):
              self.initial['Rules'] = eval(self.initial['Rules'])
0

yes you can do that. You need to use the MultipleChoiceField field with a CheckboxSelectMultiple widget to do that. I think you are doing it right but maybe you forgot () in widgets?

class Segmentation_Form(forms.Form):
    Rules = forms.MultipleChoiceField(choices= Segmentation_Rules_CHOICES, widget=forms.CheckboxSelectMultiple())

    def clean_Rules(self):
        if len(self.cleaned_data['Rules']) > 3:
            raise forms.ValidationError('Select no more than 3.')
        return self.cleaned_data['Rules']

I have thrown in a validation method. Where you can have a limit on number of choices selected.

2
  • No it doesn't work for me , I have define choices in my models not in forms , is this the reason ??
    – Inforian
    Jan 30, 2013 at 6:43
  • no even i defined CHOICES in my models only. but forms need to be aware of this. since the widget works based on that. Jan 30, 2013 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.