Alex Rivera | Logout

Django: How to build a custom form widget?

Asked 2011-01-16T18:27:04.033
68

I am having a difficult time finding documentation on how to write a custom widget.

My questions are:

  • If I build a custom widget, can it be used equivalently for the admin interface or for normal forms?
  • If I want to allow the user to edit a list of items, what widget should I subclass? What methods of the widget do I need to override/implement?
  • What widget method is responsible for going from the user's input back to the data model?

Thanks.

Edit
Report

1 Answer

7

NOTE: There are three questions here. For the first two questions, see the fuller answer by AndiDog. I'm only answering the third question here:

Q. What widget method is responsible for going from the user's input back to the data model?

A. The value_from_datadict method -- it's sort of the inverse of a widget's render method. This method is presumably what the Django docs on widgets are referring to when it says "The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget." There's nothing further on this point in the docs, but you can see how it works from the code for the built-in widgets.

answered 2012-07-18T18:02:12.553

Your Answer