This class extends MultiWidget to create a widget that consists of HTML select elements for Django's forms.DateTimeFields. This results in a select elements with options for month, day, year, hour, minute, second, and (if using the twelve_hr option) meridiem.
# Default usage of SplitSelectDateTimeWidget
class TimeForm(Form):
dt = DateTimeField(widget=SplitSelectDateTimeWidget())
Another example hooks into the flexibility of the underlying Select Widgets:
class TimeForm(Form)
dt = DateTimeField(widget=SplitSelectDateTimeWidget(hour_step=2, \
minute_step=15, second_step=30, twelve_hr=True, years=[2008,2009,2010]))
The above example displays hours in increments of 2, minutes in increments of 15, and seconds in increments of 30. Likewise, only the years 2008, 2009,and 2010 are displayed in the years' options.
A Textarea widget which appends basic Textile formating instructions in the same way Basecamp's Writboard product displays some basic helper markup alongside the edit area.
This patch allows to open a popup to edit the selected object of either the left or right select input of a filter_horizontal or filter_vertical field, when pressing the "Insert" key.
Being a noob with general client-side issue, it's served as a patch that suits my needs. Maybe it's possible to do it cleaner by overloading the javascript methods, or get rid of the jquery dependency in order to integrate it into django trunk ... Any contribution is welcome! It's licensed under WTFPL license.
Requires jquery.
These field and widget are util for to those fields where you can put a star and end values.
It supports most of field types and widgets (tested with IntegerField, CharField and DateField / TextInput and a customized DateInput).
**Example of use:**
class FormSearch(forms.Form):
q = forms.CharField(max_length=50, label=_('Search for'))
price_range = RangeField(forms.IntegerField, required=False)
**Example of use (with forced widget):**
class FormSearch(forms.Form):
q = forms.CharField(max_length=50, label=_('Search for'))
price_range = RangeField(forms.IntegerField, widget=MyWidget)
This replaces the html select box for a foreign key field with a link to that object's own admin page. The foreign key field (obviously) is readonly.
This is shamelessly based upon the [Readonly admin fields](http://www.djangosnippets.org/snippets/937/) snippet. However, that snippet didn't work for me with ForeignKey fields.
from foo.bar import ModelLinkAdminFields
class MyModelAdmin(ModelLinkAdminFields, admin.ModelAdmin):
modellink = ('field1', 'field2',)
Django administration provides three buttons for submitting the currently edited object. Each of them has a unique name and depending on the name that is sent to the server, the specific action is performed. I see this as an ugly solution and prefer to have a choice field in the form which would render as submit buttons with different values. Then the values would be checked instead of the names. Therefore, I created the `MultipleSubmitButton` widget. When `<input type="submit" value="Go" />` is used, the value sent to the server always matches the text on the button, but if `<button type="submit" value="go">Go</button>`, the value and the human representation might differ.
To use the `MultipleSubmitButton` widget, pass it to the widget parameter of a `ChoiceField` like this:
SUBMIT_CHOICES = (
('save', _("Save")),
('save-add', _("Save and Add Another")),
)
class TestForm(forms.Form):
do = forms.ChoiceField(
widget=MultipleSubmitButton,
choices=SUBMIT_CHOICES,
)
When you print `{{ form.do }}` in the template, the following HTML will be rendered:
<ul>
<li><button type="submit" name="do" value="save">Save</button></li>
<li><button type="submit" name="do" value="save-add">Save and Add Another</button></li>
</ul>
When you submit this form and check the validity of it, `form.cleaned_data['do']` will return "save" or "save-add" depending on the submit button clicked.
A FileField Widget that displays an image instead of a file path if the current file is an image. Could also be used with sorl.thumbnail to generate thumbnail images.
**Example**
class FileUploadForm(forms.ModelForm):
upload = forms.FileField(widget=AdminThumbnailWidget)
class Meta:
model = FileUpload
class FileUploadAdmin(admin.ModelAdmin):
form = FileUploadForm
admin.site.register(FileUpload, FileUploadAdmin)
Displays as an ordinary selectbox with an additional text-input for filtering the options. An adaption of the example at [1] for use with Django.
The code assumes the java script from [2] is downloaded into your media folder. Remember to output your form's media, for instance like: "{{form.media|safe}}", somewhere above your form.
**License:**
[2] by Mr Patrick Fitzgerald is licensed under GPL and the python code written by myself is GPL as well.
**References:**
1. http://www.barelyfitz.com/projects/filterlist/index.php/
2. http://www.barelyfitz.com/projects/filterlist/filterlist.js
This widget will render a chained select menu powered by JavaScript to make it easier to identify foreign keys. This widget includes danjak's form decorator (http://www.djangosnippets.org/snippets/59/), and Xin Yang's chained select javascript functions (http://www.yxscripts.com/).
I developed this to be used with an IT inventory system. See screenshot here: http://bayimg.com/cAjAGAabN
The models are laid out that location -> area -> room. But the __str__ of area and room did not include unique fields, so the built-in single select box that django uses for ForeignKey's did not work for me.
A few notes:
1: I will not be maintaining this, I am only putting it out here in case it helps others.
2: The chained select menus will only be available to the first form on the page. Reason being: the template names the form, not the django backend. So, I had to reference the form in javascript as document.forms[0].
3: Due to the javascript processing, the chain select menu will not show current values other than the default specified in the javascript. Thus, form_for_instance and a dict of values passed to form_for_model will not pre-set the chained select.
4: The rendered selects are put into a vertical table. No other layout is supported.
5: The select field names for upper-leveled options are "chain_to_[destination_field_name]__[current_selects_model_name].
6: The select name for the destination option is the name that django sends internally, which is usually the field name. The value of each option in the select is the primary key associated with that object.
7: I tried to get this built in to the native form_for_model helper function for use with the default admin site, but failed miserably.
How to get it working (quick version):
1: Define your models
2: From your view, import the form_decorator and ChainSelectWidget (I put them in CustomWidgets.py and made sure it was in the path).
3: Build arguments for the form_decorator eg:
widget_overwrite=dict(field=ChainSelectWidget(order=[(top, 'order_field'), (next, 'order_field'), (field, 'order_field)]
4: Send arguments to form_decorator eg:
callback = form_decorator(widgets=widget_overwrite)
5: Build modified form eg:
mod_formclass = form_for_model(field, formfield_callback=callback)
6: Instance the modified form eg:
instanced_form = mod_formclass()
7: Send instanced form to the templating engine
8: From the template, import the chainedselects function file (replace [] with <>) eg:
[head][script language="javascript" src="path/to/chainedselects.js"][/script]
9: Display the form object as you normally would.
There is a sample in the code.
You can use this snippet to allow your forms to easily edit object locations.
Take care about what you should add to your templates in order to make it work.
Observation: depends on jQuery to works!
This widget works like other multiple select widgets, but it shows a drop down field for each choice user does, and aways let a blank choice at the end where the user can choose a new, etc.
Example using it:
class MyForm(forms.ModelForm):
categories = forms.Field(widget=DropDownMultiple)
def __init__(self, *args, **kwargs):
self.base_fields['categories'].widget.choices = Category.objects.values_list('id', 'name')
super(MyForm, self).__init__(*args, **kwargs)
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.
The SplitTimeField and the corresponding widget SplitDateTimeWidget show two select boxes with one for hour from 0 to 23 and the other showing minutes 0,15,30 and 45 (can be customized very easily).
Usage:
-------
class TestForm(forms.Form):
start_time = SplitTimeField(widget=SplitTimeWidget)
end_time = SplitTimeField(widget=SplitTimeWidget)
A widget for selecting from a list of `Model` instances using `MultipleChoiceField` which renders a table row for each choice, consisting of a column for a checkbox followed by a column for each item specified in `item_attrs`, which must specify attributes of the objects passed as choices.