Reputation: 1736
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
Upvotes: 2
Views: 11191
Reputation: 131
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'])
Upvotes: 1
Reputation: 180
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)
Upvotes: 2
Reputation: 31548
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
Upvotes: 1
Reputation: 73588
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.
Upvotes: 0