A collection of code snippets that I use in my Django apps
Extends Django's ChoiceField to provide an empty label similar to a ModelChoiceField
- The empty_label parameter defaults to u"---------", but can be overridden
- A required field has the empty field choice prepended to the list, unless an initial value is provided.
- When the field is not required, the empty field choice is always included, regardless of whether an initial value exists
Based on https://gist.github.com/davidbgk/651080
Usage:
in forms.py:
class MyFormName(Form):
suitability = EmptyChoiceField(required=False,choices=Suitability)
See this link for a full write-up with example: http://dashdrum.com/blog/2013/02/django-emptychoicefield/
This little mixin provides an easy way to declare a custom ErrorClass for a form
Usage:
in forms.py:
class MyFormName(CustomErrorClassFormMixin, ModelForm)
error_class = MyCustomErrorListClass
Notes:
- The mixin must be declared before the form class in order to update the error_class in kwargs before the form's init() method fires
- The
error_class
attribute must be defined - See NoAsteriskTextErrorList as an example of a custom ErrorList
A simple example of defining a custom ErrorList, this class changes the default rendering example to the as_text() method, and that method is slightly modified from the stock behavior to not include an asterisk before each error.
Usage:
in views.py
form = MyFormName(data=form_data, error_class=NoAsteriskTextErrorList)
Or try the CustomErrorClassFormMixin or CustomErrorClassViewMixin
ModelBase extends the Model class and provides a couple of features I like to see in all of my models.
- The fields
created_on
andupdated_on
keep track of when the object is created or modified - The
allow_delete
property can be overridden for each model to indicate when a delete is safe. Helps avoid accidental cascading deletes.
Usage:
in models.py
class MyModel(ModelBase):
# define fields - created_on and updated_on are already defined
@property
def allow_delete(self):
if # some condition #:
return False
return True
An example of a condition that could be used in the allow_delete
function would be to check to see if there are any linked objects.
if len(MyModel.objects.get(id=self.id).anothermodel_set.all()) == 0: ## Check for linked objects
return False ## objects are linked
return True ## OK to delete
Truncates the given string to the desired length at the nearest available space.
Usage:
short_name = truncate_to_space(full_name,10)
Uses the Django provided regular expression to validate an email address.
Usage:
if is_valid_email(email_field):
send_email(email_field)
From http://tomforb.es/using-python-metaclasses-to-make-awesome-django-model-field-choices?pid=0
A metaclass that makes for simple ModelField choices
Usage:
in models.py (or wherever you wish):
class ActionType(Choice):
AWAITING_APPROVAL = 'E'
APPROVED = 'A'
REJECTED = 'R'
IN_PROCESS = 'I'
TO_COMMITTEE = 'C'
NEEDS_RESEARCH = 'N'
in forms.py:
class MyFormName(Form):
action_type = ChoiceField(required=False,choices=ActionType)
When used with a class based view descendent from FormMixin, this will include the custom error_class
value in the get_form_kwargs
method.
Usage:
views.py:
class MyViewName(CustomErrorClassViewMixin, [a FormMixin descendant, such as CreateView])
error_class = MyCustomErrorListClass
Note:
- The
error_class
attribute must be defined - See NoAsteriskTextErrorList as an example of a custom ErrorList
Set of mixins to add a 2nd model with a one-to-one relationship to class based views
Additional instance varaibles hold the information for the 2nd model:
other_model - required other_form_class other_queryset other_object other_fields
Usage:
Use these three mixins with their corresponding class-based view. Define the additional instance variables as needed.
Example:
class MyUpdateView(OneToOneUpdateMixin, UpdateView):
model=MyModel
form_class = MyForm
other_model=MyOtherModel
other_form_class = MyOtherForm
Set of mixins to add formset processing to class based views
FormsetMixin is the parent class that provides most of the functionality. However, the get() and post() methods need to be subclassed to provide a value for self.object.
FormsetCreateMixin and FormsetUpdateMixin both handle the assignment of self.object as is appropriate for their function.
Usage:
Use FormsetCreateMixin and FormsetUpdateMixin along with the corresponding class-based view. Also, the instance variable detail_form_class should be defined.
Example:
class MyView(FormsetCreateMixin, CreateView):
model = MyModel
form_class = MyModelForm
detail_form_class = MyFormest
Based on RelatedFieldWidgetWrapper, this version does the same thing outside of the admin interface.
See this link for a full write-up with example:
http://dashdrum.com/blog/2012/12/more-relatedfieldwidgetwrapper-the-popup/
Adds the ability to disable selected choices of a Select control
Usage:
in forms.py:
class MyForm(Form):
def __init__(self, disabled, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
if disabled:
self.fields['my_field'].widget.disabled = disabled
my_field = ModelChoiceField(widget=SelectDisabled(),queryset=MyModel.objects.all(),empty_label=None)
in views.py:
## Set a condition in the filter clause of the query to return
## the disabled items
disabled = []
for m in MyModel.objects.filter(##condition##).values_list('id'):
disabled.append(m[0])
form = MyForm( data=request.POST or None, disabled=disabled)
Based on code from a docs page of django-authtools, this app will mod the user forms in Admin to not ask for a password. This would be used when authentication is expected to come from outside, such as LDAP or SAML.
http://django-authtools.readthedocs.io/en/latest/how-to/invitation-email.html
Usage:
include 'snips.no_password', in the list of INSTALLED_APPS
Note that the createsuperuser management command is not affected by this app, and the administrator can still create a local superuser account with a password.