ModelChoiceTitleField is a ModelChoiceField descendent that creates <OPTIONS> with title elements based on the field specified in title_source_field:
priority=ModelChoiceTitleField(Priority.objects.all(),
initial=Priority.objects.get(default=True).id,
title_source_field='long_description')
That will generate a `<SELECT>` element looking like:
<select name="priority" id="id_priority">
<option value="1" title="Some extremely important task.">Emergency</option>
...
</select>
In the `<option>` above, the title was retrieved from the `long_description` field for the instance of the Priority class. The word Emergency came from a call to the instance of the Priority class' `__unicode__()` method. The value of the option is the PK for the instance of the Priority class.
This is a decorator which essentially replaces the decorated view with a view that always raises `Http404` (File Not Found) when `settings.DEBUG` is set to True.
You've filtered your changelist in your admin site and you want to edit a few entries here; you click on an object, edit it and press save, and you end up back at the default unfiltered changelist view. This ModelAdmin override is so that if you press "save" (not "save and add another", or "save and continue editing") you end up back at your filtered changelist.
There are other ways out there and other snippets to do similar; however I hadn't seen one to only redirect if you pressed save so this is what I came up with. Hopefully it's useful.
Deploying relocatable Django sites isn't currently as trivial as it should be (see http://code.djangoproject.com/ticket/8906, http://groups.google.com/group/django-developers/tree/browse_frm/thread/fa3661888716f940/). This snippet relocates all url patterns (similarly to http://djangosnippets.org/snippets/2129/) as well as the absolute url settings of `settings.py`.
This allows deployment under a different mount point with a single Django setting, without having to repeat the mount point again as a SCRIPT_NAME parameter supplied by the web server.
I needed to create a feature for administrators to create accounts and email the *new account was created* email using a button. Remember to change the default template and subject to match your needs.
The code is directly taken from `django.contrib.auth.forms.PasswordResetForm.save()`.
Notice that the function raises `ValueError` if the user is missing an email address.
You can convert any string to a QR code image as easy as use a simple filter, thanks to google charts api.
Common use:
<img src="{{object.attribute_to_encode|qr:"120x130"}}" />
This will create a 120(width) x 130(heiht) image with the value of the attribute_to_encode encoded in a QR coded image.
I wanted a way to deploy a Django site to both the root of a domain, and to a subdirectory. The solution was to loop over all urlpatterns and add a configurable string (URL_PREFIX) at the beginning of all patterns.
Point '^accounts/login/$' or whatever your custom login path is to the 'negotiate_ntlm' view.
This allows you to keep anonymous authentication enabled on IIS and easily lock down just the parts of the site you need to (e.g. [admin](http://djangosnippets.org/snippets/2127/)).
Forces admin site to use your custom login.
Very useful when using RemoteUserBackend.
See [here](http://djangosnippets.org/snippets/2128/) for a use case.
Based on [Extended Profiling Middleware](http://djangosnippets.org/snippets/605/), this version allows interactive sorting of functions and inspection of SQL queries.
***About***
I tried to dump data from my database (manage.py dumpdata) and I couldn't do it because of error:
User matching query does not exists
I found out that my database was filled with garbage: entries those foreigners were deleted. My table's engine is MyISAM so it allows for these lost entries to exist. I had to cleanup my database before I do datadump, so I've written a script which worked fine for me.
***Usage***
Place this script in same directory with your settings.py and run it: `python db_cleanup.py`
***Disclaimer***
Backup your data :)