How to change ManyToManyField widget to CheckboxSelectMultiple without overriding field definition in a ModelForm
-
I have django ModelForm for model with ManyToManyField. I want to change widget for this field toCheckboxSelectMultiple. Can I do this without overriding a field in a form definition? I constantly use code similar to this: class MyModel(ModelForm): m2m_field = forms.ModelMultipleChoiceField(queryset = SomeModel.objects.all(), widget = forms.CheckboxSelectMultiple()) Is there other way to do this? EDIT: I need this for Django 1.1.1 project
-
Answer:
If you're using Django 1.2+, you can use the widgets tuple in the inner Meta class. class MyModelForm(forms.ModelForm): class Meta: widgets = {'m2m_field': forms.CheckboxSelectMultiple} See http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets.
Lukasz Dziedzia at Stack Overflow Visit the source
Other answers
Another way to do this is doing it in the init of the ModelForm: class MyModel(ModelForm): def __init__(self, *args, **kwargs): super(MyModel, self).__init__(*args, **kwargs) self.fields['m2m_field'].widget = forms.CheckboxSelectMultiple() [...]
patrick
Related Q & A:
- How do I change the widget clock to central time?Best solution by Yahoo! Answers
- How to change my yahoo ID without changing my email address?Best solution by Yahoo! Answers
- How can I sell something on ebay without using a credit card?Best solution by Yahoo! Answers
- Can you change your yahoo email address without creating a new account?Best solution by Yahoo! Answers
- How to change your name on Facebook without it being rejected?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.