Subclass of the ModelForm which allows to make fields 'display_only'.
This means no formfield will be displayed, but a suitable representation. You can make all fields display_only or just a few (or you can use the form as a normal modelform).
There are also some extra's to easily set attrs on fields or set help_texts on widgets when using this form.
Why ?
I made my own set of generic crud views based on newforms, but added a 'display' view to simply display objects, in the same table layout as the editing is done, but without the fields. I wanted to avoid having to redefine my forms twice and I wanted to reuse some generic templates as much as possible.
Obviously this is not good for performance, but I use it for an intranet app with a lot of objects, but not that big a load.
Their is definitely still a lot of room for improvement, and maybe all this could have been done much easier.
How to use?
See the docstring.
**The problem**
ModelChoiceField always uses __unicode__ or __str__ to fill the labels. I needed to dynamically select which field to use for the labels.
**The solution**
My approach copies a lot from [this blog](http://oebfare.com/blog/2008/feb/23/overriding-modelchoicefield-labels/) with some modifications to make it more dynamic.
There are some proposals to fix on this [Ticket #4620](
http://code.djangoproject.com/ticket/4620)
**How to use**
Include the code on your forms.py (or whatever you are using) and use the CustomChoiceField with the extra argument label_field instead of ModelChoiceField.
Hope it helps someone.
Based on [danjak's](http://www.djangosnippets.org/users/danjak/) [snippet](http://www.djangosnippets.org/snippets/99/) but updated to use ModelForms - so can easily handle generic CRUD operations.
A replacement create_update.py for use with ModelForm
create_object and update_project modified to handle newforms (including FileFields) with ModelForm - it also had delete_object as well for completeness.
In addition, it has some extras:
* extra_fields - this is a dict or callable that contains additional fields to be passed to the form, for example stuff that is in the session.
* on_success - callback called if form is valid and object created/updated, if this is not set the default behaviour is to send a redirect
* on_failure - callback called if form is invalid, the default is to redisplay the form.
Note that once newforms are finally done these functions are likely to be redundant, as generic views will be updated to use the newforms API, so use with caution!
ModelChoiceField allows you to use filtered queries to simplify your forms. This is great for adding objects but can fall down when you edit an existing object and the original query no longer contains the referenced field (e.g. I like to use an "active" field in several objects).
The fix is simply to include an extra param: Q(pk=object_id). You have to do this in the __init__ method to get the object_id.
A nice thing about this is that it works for ModelForms as well as custom Forms.