If you are using django-mptt to manage content (eg heirarchical categories) then it needs a bit of help to make a nice admin interface. For many-to-many fields, Django provides the quite nice FilteredSelectMultiple widget (a two-pane selection list with search box) but it only renders 'flat' lists... if you have a big category tree it's going to be confusing to know what belongs to what. Also, list items are sorted alphabetically in the js, which won't be what you want.
This snippet extends FilteredSelectMultiple to show the tree structure in the list.
You'll also need the js from this snippet: [#1780](http://www.djangosnippets.org/snippets/1780/)
For usage details see my blog at:
[http://anentropic.wordpress.com](http://anentropic.wordpress.com/2009/11/05/more-django-mptt-goodness-filteredselectmultiple-m2m-widget/)
Shows field value as plain text which can't be edited by user. Field value (or key value for foreign keys) is stored in hiddden input.
Value of field is stored in hidden input and current value is placed as plain text. User can't change it's value. If field is foreign key then additional attribute 'key' should be set to True (key is stored in hidden field and unicode value is visible):
self.fields['my_field'].widget = HiddenInputWithText(attrs={ 'key' : True })
There is one condition: for foreign key field its name have to be same as lowercased model name.
Default 'key' value - False is correct for non-foreign key fields:
self.fields['my_field'].widget = HiddenInputWithText()
Widget for TinyMCE 3.2.6, a WYSIWYG HTML editor for `textarea`.
**Note:**
> This snippet uses the TinyMCE package thats contains special jQuery build of TinyMCE and a jQuery integration plugin. Anyway, is easily to adapt to standard package.
Usage example:
from django.contrib.flatpages.admin import FlatpageForm
class MyFlatPageForm(FlatpageForm):
content = forms.CharField(widget=TinyMCEEditor())
[TinyMCE download page](http://tinymce.moxiecode.com/download.php)
Ripped this out of a project I'm working on. The field renders as two <select> elements representing the two-level hierarchy organized events Facebook uses. Returns the id's Facebook wants.
DateTimeWidget using [JSCal2](http://www.dynarch.com/projects/calendar/)
Duplicate of [this snippet](http://www.djangosnippets.org/snippets/391/), but for latest 1.5 version of DHTML Calendar.
Also here is **fixed problem of previous widget** linked to *form.changed_data* and *EntryLog.message*. This is fixed by adding own, little modified *_has_changed()* method
This is an example of how you change a ChoiceField select widget into a hidden field if the right GET variable is passed. In this example code it would change the select widget into something like the following if something like "?d=3" was passed.
`<p><label for="id_designation">Designation</label>Designation Option<input type="hidden" name="designation" value="3" id="id_designation" /></p>`
AdminImageWidget is a ImageField Widget for admin that shows a thumbnail.
Usage example on a form:
class IconForm(forms.ModelForm):
icon = forms.ImageField(label='icon', widget=AdminImageWidget)
This Field, Widget and the three lines of code in the form's clean method allow an easy (and hopefully general) way to add simple, unchangable text to a form.
Example use:
class OnlyTextForm(forms.ModelForm):
points = StaticField(label=_('Points'))
reviewer = StaticField(label=_('Reviewer'))
def clean(self):
for name, field in self.fields.items():
if isinstance(field, StaticField):
self.cleaned_data.update({name: self.initial[name]})
return self.cleaned_data
This code creates a widget that we used to generate a list of radiobuttons as follows:
* Radio button 1
--Widget__1
* Radio button 2
--Widget__2
* Radio button 3
--Widget__3
How to use it is:
`ImagenForm class (forms.ModelForm):
widget1 = forms.BooleanField ()
widget2 = forms.TextInput
widget3 = forms.ModelChoiceField (queryset = Modelo.objects.all ())
widget4 = forms.FileInput
optConListas = ChoiceWithOtherField (choices = [(2, 'widget1' widget1), (3, 'widget2' widget3.widget), (4, "widget2" widget2), (5, 'widget4' widget4)])`
There are probably ways to improve the implementation, but this was something I came up with when I wanted to change the default size of all of my CharField admin fields. Now all I have to do in my ModelAdmin class is:
form = get_admin_form(model)
or subclass BaseAdminForm if I need extra validation or more widget customization for an individual admin form.