We currently use two-level tuples to specify choices of a field in models or forms.
But, because it has only (value, verbose name) pair, the readability is bad whenever we indicate a specific choice value in our Python codes.
So I made a small class that does "magic" for this: A Named Enumeration.
Instead of `myobj.status == 0`, use `myobj.status == STATUS.UNREVIEWED`, for example.
This is a ModelAdmin base class you can use to make foreign key references to User a bit nicer in admin. In addition to showing a user's username, it also shows their full name too (if they have one and it differs from the username).
**2009-08-14**: updated to handle many to many fields and easily configure whether to always show the username (if it differs from full name)
TestableTemplate behaves just like django.template.Template, but you can give it a list of template.Libraries to load before parsing the template. This is equivalent to adding a bunch of {% load %} tags to the beginning of your template string, but you can use custom tag libraries which do not belong to Django applications' templatetags packages.
This is occasionally useful in testing.
This is a rewrite of [snippet #1006](http://www.djangosnippets.org/snippets/1006/) to use the moderation features available in Django's comments framework. This is more customizable than the signals approach and works well if other moderation features are being used. If you want to make comments that are flagged as spam become hidden instead of deleted, change the allow() method to moderate(). [See the blog post here](http://sciyoshi.com/blog/2009/jul/17/prevent-django-newcomments-spam-akismet-reloaded/)
On a busy site it can be nice to have a summary of admin activity. Running this command (I call it "adminlog") generates output like this:
2009-07-10 18:06:19: pbx changed flat page: "/yay/ -- Let's All Say Yay"
By default it shows the last five actions; pass it a numerical arg to show more or fewer.
Run this as a cron job and you can follow a site's admin-side activity without even logging in!
A confirm alert is displayed if the user has made any changes to the form, and attempts to navigate away from the page without saving (click the back button, hit reload, click the breadcrumbs, logout, etc). Much like GMail's "Your message has not been sent. Discard your message?" prompt.
**Uses the JavaScript helpers built into Django**, without relying on 3rd party libs like jQuery. (There might be simpler options if you're already using [jQuery](http://code.google.com/p/protect-data/) or [prototype](http://stackoverflow.com/questions/925111/activating-onbeforeunload-only-when-field-values-have-changed/1013033#1013033)).
To use this, simply create a [custom admin template](http://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates). For example at:
*templates/admin/YOUR_APP_NAME/change_form.html*
{% extends "admin/change_form.html" %}
{% block after_related_objects %}
{{ block.super}}
<script type="text/javascript">
... script goes here ...
</script>
{% endblock %}
A one-liner that I use all the time: Set `upload_to` to something based on the slug and the filename's extension.
Just add this function to the top of your models and use it like this:
image = models.FileField(upload_to=slug_filename('people'))
and the `upload_to` path will end up like this for eg `myfile.jpg`:
people/this-is-the-slug.jpg
Enjoy!
Simple Python snippet to detect if any word in a list of words is inside your string. Use for profanity checking (my use case), auto tag detection, scoring, etc.
This will return an empty list if the word is not in the list. Assumes everything in words_to_find is lower case. Can probably be done cleaner with regular expressions but this method is extremely readable for those that prefer none regex solutions.
The widget for FileField and ImageField has a problem: it doesn't supports clear its value and it doesn't delete the old file when you replace it for a new one.
This is a solution for this. It is just for Admin, but you can make changes to be compatible with common forms.
The jQuery code will put an **<input type="checkbox">** tag next to every **<input type="file">** and user can check it to clear the field value.
When a user just replace the current file for a new one, the old file will be deleted.