A [common pattern in Django](http://code.djangoproject.com/wiki/UsingMarkup) is to create a TextField intended for Markdown text (i.e. description) and a companion non-editable TextField for storing the HTML version (i.e. description_html), so the Markdown converter need not be run for every page view.
This snippet is a custom field which encapsulates this pattern in a single field which can automatically create and update its companion HTML field.
Usage:
class MyModel(models.Model):
description = MarkdownTextField()
These three classes allows you to use enumerations (choices) in more natural model-like style. You haven't to use any magic numbers to set/get field value. And if you would like to make your enumeration a full-fledged django-model, migration should be easy.
Note, that you can subclass Item to add some context-specific attributes to it, such as `get_absolute_url()` method for instance.
Examples are provided in the form of `doctest` in the second half of the snippet.
Simple DecimalField class extension that automatically adds formatting and validation for comma-separated "decimals". Works wonderfully for price fields.
Could be extended to strip dollar signs or to be locale-agnostic.
Extension to the normal ManyToManyField to support default values.
Build for the following use case:
publish_on = ManyToManyFieldWithDefault(Site, verbose_name=_('publish on'), default=Site.objects.get_current)
Where with a plain ManyToManyField the default site will not be selected. The ManyToManyFieldWithDefault fixes this by automatically selecting the default value if no other selections are given.
When the field also have null=True and Blank=True it will not select the default !
Custom field for using MySQL's `text` type.
`text` is more compact than the `longtext` field that Django assigns for `models.TextField` (2^16 vs. 2^32, respectively)
Some functions and newforms fields for validating credit card numbers, and their expiry dates.
In my project, I have all of the credit card functions in a file called creditcards.py
Just as an overview: To validate a credit card number there are a few steps:
1. Make sure the number only contains digits and spaces. `ValidateCharacters()`
2. Remove spaces so that only numbers are left. `StripToNumbers()`
3. Check that the number validates using the Luhn Checksum `ValidateLuhnChecksum()`
4. Check to see whether the number is valid for the type of card that is selected. This is annoying because you will need to look at another cleaned field before you can check this.
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.
You can add this code to a file named "field_attrs.py" in a templatetags folder inside an application.
To use it, remember to load the file with the following template tag:
{% load field_attrs %}
And for each field you want to change the widget's attr:
{{ form.phone|attr:"style=width:143px;background-color:yellow"|attr:"size=30" }}
New field type which allows prepopulate_from to work not only from javascript but in python too.
If the slugfield has unique=True creates a unique slug too.
Automatically sets date to today if only time part was entered.
If today is 01/01/2008, then both DateTimeField and TodayDateTimeField clean '2008-01-01 15:05' to datetime(2008,01,01,15,5,0), while '15:05' is cleaned as datetime(1900,01,01,15,5,0) for DateTimeField but datetime(2008,01,01,15,5,0) for TodayDateTimeField.
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.
**This is a newforms field for XFN relationships.**
It normalizes input by removing excess whitespace, converting to lowercase and removing duplicates.
This field also validates the relationship according to the [XFN profile](http://gmpg.org/xfn/11): `me` can only appear by itself and, at most, one value from each of the family, friendship and geographical categories is allowed.
The `XFN_*` constants would probably be imported from somewhere else in practice, but are included here for simplicity.
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)
**This is a newforms field for OpenID 1 & 2.**
It normalizes and validates OpenID identifiers according to the [spec](http://openid.net/specs/openid-authentication-2_0.html#normalization):
* `xri://=joelwatts` to `=joelwatts`
* `joelwatts.com` to `http://joelwatts.com/`
* `www.joelwatts.com` to `http://joelwatts.com/`
An identifier that is well-formed, but not an OpenID (e.g. `example.com`), will cause a validation error.
Requires [Python OpenID Library](http://openidenabled.com/python-openid/) 2.x.x.
*Incredibly useful for storing just about anything in the database (provided it is Pickle-able, of course) when there isn't a 'proper' field for the job:*
A field which can store any pickleable object in the database. It is database-agnostic, and should work with any database backend you can throw at it.
Pass in any object and it will be automagically converted behind the scenes, and you never have to manually pickle or unpickle anything. Also works fine when querying.
**Please note that this is supposed to be two files, one fields.py and one tests.py (if you don't care about the unit tests, just use fields.py)**