If you try to use multiple inheritance with a modelform (to mix in some fields from an already existing form class for example) you'll get the following rather terrifying error:
> "Error when calling the metaclass bases metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases"
The solution is to first create the ModelForm, then create a NEW class that inherits from both the ModelForm and the form you want to mixin, then finally apply the recipe from here: [http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204197](http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204197)
django.contrib.formtools in preview displaying only field.data by default. Its not convenient to see integer value for fields with radio buttons or select choices.
This custom tag trying to show string value from choices if available.
This template tag and suggested template allow greater control over rendering `<label>` tags for your newforms fields than using `field.label_tag`. I save the provided Python code in my app as `templatetags/forms.py` (although this name may conflict in the future).
The simplest usage:
{% label field %}
One use case is adding `class="required"` to the label tag for required fields instead of inserting markup elsewhere--this is done in the given example template.
Alternate label text and tag attributes can be passed to the inclusion tag:
{% label field "Alt. label" 'class=one,id=mylabel' %}
NOTE: this is for **newforms-admin**
I need edit links for items on my site, outside of the django admin -- however, I'm lazy, and don't want to build my own edit forms when there's a perfectly nice admin already there.
Trick is, I need to be able to click a link to the django admin edit page for an item, and have it return to the calling page after saving.
This chunk of code does the trick (the "real" version extra cruft, naturally) -- the links will bring up the django admin editor, then return to the calling page after saving.
A quickie clone of good old fashion [Formmail.pl](http://www.scriptarchive.com/formmail.html) any form that is a subclass of FormMail will have its conents emailed to all staff members on the site.
Inherit your forms from model from this ModelForm and it will check all the database fields with unique=True in `is_valid()`.
This is a hack around [#5736](http://code.djangoproject.com/ticket/5736). It is actually a part of a grand problem mentioned in [#4895](http://code.djangoproject.com/ticket/4895). You can use this hack until the issue is fully resolved.
**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.
This might be handy in countries where decimals are entered with a comma separating the decimal places from the integer part (for instance in Germany). It lets user enter and displays all decimals with a comma separator.
I ran into this problem and couldn't find a clean internationalized way of doing it... but newforms makes it so easy to roll your own. Hope it helps someone.
A TimeField that lets you parse a wide variety of freeform-text time descriptions. This doesn't inherit from TimeField because it doesn't use any of its functionality.
Includes unit tests demonstrating some examples of what the parser will and won't handle.
Example model:
class MyModel(models.Model):
file = RemovableFileField(upload_to='files', \
null=True, blank=True)
image = RemovableImageField(upload_to='images', \
null=True, blank=True)
A delete checkbox will be automatically rendered when using ModelForm or editing it using form_for_instance. Also, the filename or the image will be displayed below the form field. You can edit the render method of the DeleteCheckboxWidget or add one to RemovableFileFormWidget to customize the rendering.
UPDATE: 5. April 2009. Making it work with latest Django (thanks for the comments).
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.
The class LocationField renders a form field with map (from google maps) and a mark. It allows the user to drag the mark to point at some particular location, whose value (lat, lng) is saved in a hidden field.
It requires jquery and google maps.
I love newforms. But sometimes using ``{{ form }}`` within a template doesn't give you enough flexibility. The other option, manually defining the markup for each field, is tedious, boring and error-prone.
This is an example of how you can use a template filter to get the best of both worlds. Use it like this to render an entire form:
``{% for field in form %}``
{{ field|form_row }}
``{% endfor %}``
Or use it on a per-field basis:
``<fieldset>``
{{ form.first_name|form_row }}
{{ form.last_name|form_row }}
``</fieldset>``