Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you change the default widget for all Django date fields in a ModelForm?

Given a set of typical models:

# Application A from django.db import models class TypicalModelA(models.Model):     the_date = models.DateField()   # Application B from django.db import models class TypicalModelB(models.Model):     another_date = models.DateField()  ... 

How might one change the default widget for all DateFields to a custom MyDateWidget?

I'm asking because I want my application to have a jQueryUI datepicker for inputting dates.

I've considered a custom field that extends django.db.models.DateField with my custom widget. Is this the best way to implement this sort of across-the-board change? Such a change will require specifically importing a special MyDateField into every model, which is labour intensive, prone to developer error (i.e. a few models.DateField's will get through), and in my mind seems like unnecessary duplication of effort. On the other hand, I don't like modifying what could be considered the canonical version of models.DateField.

Thoughts and input is appreciated.

like image 454
Brian M. Hunt Avatar asked Mar 19 '09 03:03

Brian M. Hunt


People also ask

How do I customize Modelan in Django?

To create ModelForm in django, you need to specify fields. Just associate the fields to first_name and lastName. Under the Meta class you can add : fields = ['first_name','lastName']. @Shishir solution works after I add that line. or you can try solution in Jihoon answers by adding vacant fields.

What a widget is Django how can you use it HTML input elements?

A widget is Django's representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. The HTML generated by the built-in widgets uses HTML5 syntax, targeting <!

What is widget in forms?

Form widgets allows users to input data and provides them interaction capability with the application. Every Form widget inherits properties from Widget class which in turn inherits properties from UIObject and Wigdet classes.

Which code will give us a textarea form field in Django?

CharField() is a Django form field that takes in a text input. It has the default widget TextInput, the equivalent of rendering the HTML code <input type="text" ...> .


1 Answers

You can declare an attribute on your ModelForm class, called formfield_callback. This should be a function which takes a Django model Field instance as an argument, and returns a form Field instance to represent it in the form.

Then all you have to do is look to see if the model field passed in is an instance of DateField and, if so, return your custom field/widget. If not, the model field will have a method named formfield that you can call to return its default form field.

So, something like:

def make_custom_datefield(f):     if isinstance(f, models.DateField):         # return form field with your custom widget here...     else:         return f.formfield(**kwargs)  class SomeForm(forms.ModelForm)     formfield_callback = make_custom_datefield      class Meta:         # normal modelform stuff here... 
like image 101
James Bennett Avatar answered Sep 25 '22 22:09

James Bennett