91
In terms of the forms library, you would use the MultipleChoiceField field with a CheckboxSelectMultiple widget to do that. You could validate the number of choices which were made by writing a validation method for the field:
class MyForm(forms.Form):
my_field = forms.MultipleChoiceField(choices=SOME_CHOICES, widget=forms.CheckboxSelectMultiple())
def clean_my_field(self):
if len(self.cleaned_data['my_field']) > 3:
raise forms.ValidationError('Select no more than 3.')
return self.cleaned_data['my_field']
To get this in the admin application, you’d need to customise a ModelForm and override the form used in the appropriate ModelAdmin.
0
@JonnyBuchanan gave the right answer.
But if you need this in the django admin for many models, and you’re (like me) too lazy to customize a ModelForm and ovverride the right methods inside the ModelAdmin class, you can use this approach:
http://www.abidibo.net/blog/2013/04/10/convert-select-multiple-widget-checkboxes-django-admin-form/
- [Django]-Sending HTML email in django
- [Django]-Extend base.html problem
- [Django]-Can you perform multi-threaded tasks within Django?
Source:stackexchange.com