This FloatField replacement allows users to enter math expressions, such as:
4/5 + sqrt(32)
And will evaluate them safely when the field's clean() function is called. In the example above, it will evaluate to a float value of about 6.457.
Reference:
[http://lybniz2.sourceforge.net/safeeval.html](http://lybniz2.sourceforge.net/safeeval.html)
The available functions are listed herein. Note that the from __future__ import division causes integer division expressions to be evaluated as floats. For example "1/2" evaluates as 0.5 when it would otherwise have evaluated to 0 (assuming Python 2.X).
Model field that stores serialized value of model class instance and returns deserialized model instance. Example usage:
from django.db import models
import SerializedObjectField
class A(models.Model):
object = SerializedObjectField(serialize_format='json')
class B(models.Model):
field = models.CharField(max_length=10)
b = B(field='test')
b.save()
a = A()
a.object = b
a.save()
a = A.object.get(pk=1)
a.object
<B: B object>
a.object.__dict__
{'field': 'test', 'id': 1}
OrderField for models from http://ianonpython.blogspot.com/2008/08/orderfield-for-django-models.html and updated to use a django aggregation function. This field sets a default value as an auto-increment of the maximum value of the field +1.
This is a replacement for Django's built-in ImageField. It uses the Google AppEngine image APIs in order to validate.
Notes:
1. Validation of the field counts against your App Engine transformations quota.
2. This code assumes you're only using the in-memory file upload handler. None of the other stock handlers work well on App Engine; you should probably disable them.
Sometimes it is useful to have a ChoiceField which calculates its choices at runtime, when a new instance of a form containing it, is generated. And this is what `LazyChoiceField` does.
The `choices` argument must be an *iterable* as for `ChoiceField`.
**Usage example**
from django import forms
DynamicApplicationList = []
class MyForm(forms.Form):
dynamic_choice = LazyChoiceField(choices = DynamicApplicationList)
`DynamicApplicationList` can now be updated dynamically.
This is a username field that matches (and slightly tightens) the constraints on usernames in Django's `User` model.
Most people use RegexField, which is totally fine -- but it can't provide the fine-grained and user friendly messages that come from this field.
Here is a way to get a drop down list from a queryset, with a list of "featured" items appearing at the top (from another queryset). This can be used for long select boxes which have a subset of commonly used values. The empty label is used as a separator and values can appear in both the featured set and the full set (it's more usable if they are in both).
For example a country drop down list with 5 featured countries might look like this:
Andorra
Australia
Kazakhstan
Singapore
Turkey
------------
Afghanistan
Albania
Algeria
American Samoa
Andorra
Angola
(hundreds more)
To use this, define your form field like this:
country = FeaturedModelChoiceField(queryset=Country.objects.all(),
featured_queryset=Country.objects.featured())
This is a custom field that lets you easily store JSON data in one of your model fields. This is updated to work with Django 1.1.
**Example: (models.py)**
from django.db import models
import JSONField
class MyModel(models.Model):
info = JSONField()
** Example: (shell)**
>>> obj = MyModel.objects.all()[0]
>>> type(obj.info)
<type 'NoneType'>
>>> obj.info = {"test": [1, 2, 3]}
>>> obj.save()
**[Code at GitHub](http://github.com/bradjasper/django-jsonfield/tree/master)**
For disabling autocomplete and security purpose, this snippet defines a CharField with a randomness name for each request of the form.
This is useful for turning off autocomplete for credit card input in all browsers, without breaking the xhtml validation.
* [https://wiki.mozilla.org/The_autocomplete_attribute_and_web_documents_using_XHTML#Security](https://wiki.mozilla.org/The_autocomplete_attribute_and_web_documents_using_XHTML#Security)
* [http://en.wikipedia.org/wiki/Cryptographic_nonce](http://en.wikipedia.org/wiki/Cryptographic_nonce)