Is there any good articles that explain custom form fields in django, not custom model fields? I couldn't find any through google.
4 Answers
Form fields are easy to customize:
class UpperCaseField(forms.CharField):
def clean(self, value)
try:
return value.upper()
except:
raise ValidationError
basically you just create a class that inherits from the field that most resembles what you want, then rewrite the clean() method so that it returns the value you want. Here is another example:
class MyObjectField(forms.ModelChoiceField):
# in this case, 'value' is a string representing
# the primary key of a MyObject
def clean(self, value):
try:
return MyObject.objects.get(pk=value)
except:
raise ValidationError
custom widgets on the other hand, are a little more useful, but a little more hard to do because there are a few more methods that need to be written so that they work smoothly.

- 33,060
- 24
- 83
- 117
-
1Thank you. I think I need to write a custom widget because I need to customize the rendering of the field. – Mert Nuhoglu Oct 09 '09 at 06:57
-
9Note: clean() is responsible for running to_python(), validate() and run_validators(). If you override clean() and don't implement these, you'll mess things up. – orokusaki Jan 22 '10 at 02:06
-
4orokusaki is right, if you override clean(), you should at least call it's super implementation with `super(UpperCaseField, self).clean(value)` – André Staltz May 14 '13 at 14:35
As always with open-source code, you'll learn a great deal by reading the source itself. See the django.forms.fields
module to see how all the different form fields are defined - most of them are subclasses of others already, so you can just replicate that and change what you need.

- 588,541
- 66
- 880
- 895
-
8A link to the latest source: https://github.com/django/django/blob/master/django/forms/fields.py – Ben Regenspan Dec 18 '12 at 00:42
It's not a tutorial, but django's docs talks about this a little:
If the built-in Field classes don't meet your needs, you can easily create custom Field classes. To do this, just create a subclass of django.forms.Field. Its only requirements are that it implement a clean() method and that its
__init__()
method accept the core arguments mentioned above (required, label, initial, widget, help_text).
You can read about the clean method and see an example at the django docs. Again, not a tutorial, but useful.
I find I am learning a lot by reading the code in some of the the django app projects that are available, such as django-extensions, which override the form fields and are good learning tools (for me, at least). This can help get you started.
I found these tutorials helpful:
As you have already mentioned, the Django documentation has a section How to create custom model fields which unfortunalety deals only with model fields, and moreover, it is not very comprehensive.
As Daniel already recommended, you can read throught the source code of the django.forms.fields
module to see how all the different form fields are defined, and this might help you learn how to define new ones.
You can also learn a lot from custom form field examples here on Stack Overflow, such as Custom hidden field, Custom password field, Custom form field, and Custom tag field.
Note: I have added all good resources that I know of, if someone knows about other. Please let me know in the comments and I will add them.

- 303
- 1
- 8