Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ModelForm override widget

Disclaimer: I am a beginner with python and Django but have Drupal programming experience.

How can I override the default widget of this:

#models.py class Project(models.Model): color_mode = models.CharField(max_length=50, null=True, blank=True, help_text='colors - e.g black and white, grayscale') 

in my form with a select box? Is the following OK or am I missing something?

#forms.py from django.forms import ModelForm, Select class ProjectForm(ModelForm):     class Meta:         model = Project         fields = ('title', 'date_created', 'path', 'color_mode')         colors = (                    ('mixed', 'Mixed (i.e. some color or grayscale, some black and white)'),                    ('color_grayscale', 'Color / Grayscale'),                    ('black_and_white', 'Black and White only'),                    )         widgets = {'color_mode': Select(choices=colors)} 

After reading https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets, I am lost since the example only discusses TextArea and the widgets discussion seems to exclude ModelForm.

Thanks!

like image 680
mellow-yellow Avatar asked Mar 26 '12 19:03

mellow-yellow


People also ask

How do you override a field widget in form for model?

Overriding the default fields To specify a custom widget for a field, use the widgets attribute of the inner Meta class. This should be a dictionary mapping field names to widget classes or instances. The widgets dictionary accepts either widget instances (e.g., Textarea(...) ) or classes (e.g., Textarea ).

How do I override a Django form?

You can override forms for django's built-in admin by setting form attribute of ModelAdmin to your own form class. See: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form.

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.

How do I use widgets in Django?

The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. Whenever you specify a field on a form, Django will use a default widget that is appropriate to the type of data that is to be displayed.


1 Answers

If you want to override the widget for a formfield in general, the best way is to set the widgets attribute of the ModelForm Meta class:

To specify a custom widget for a field, use the widgets attribute of the inner Meta class. This should be a dictionary mapping field names to widget classes or instances.

For example, if you want the a CharField for the name attribute of Author to be represented by a <textarea> instead of its default <input type="text">, you can override the field’s widget:

from django.forms import ModelForm, Textarea from myapp.models import Author  class AuthorForm(ModelForm):     class Meta:         model = Author         fields = ('name', 'title', 'birth_date')         widgets = {             'name': Textarea(attrs={'cols': 80, 'rows': 20}),         } 

The widgets dictionary accepts either widget instances (e.g., Textarea(...)) or classes (e.g., Textarea).

https://docs.djangoproject.com/en/3.2/topics/forms/modelforms/#overriding-the-default-fields

like image 89
qris Avatar answered Oct 11 '22 17:10

qris