djangosnippets.org: Latest snippets posted by buriyhttps://djangosnippets.org/users/buriy/2011-01-12T01:11:48.105528-06:00buriyRun model validation before saving a model instance
2011-01-12T01:11:48.105528-06:00buriyhttps://djangosnippets.org/snippets/2319/<p>How to validate your model at save using the pre_save signal.
</p>
<p>from http://groups.google.com/group/django-developers/browse_thread/thread/eb2f760e4c8d7911/482d8fd36fba4596?hl=en&lnk=gst&q=problem+with+Model.objects.create#482d8fd36fba4596
</p>
Freely redistributablekeeping Model and Field History (everywhere)
2008-09-12T15:02:17.615653-05:00buriyhttps://djangosnippets.org/snippets/1052/<p>Usage:</p>
<pre><code>class MyModel(ModelWithHistory):
class History:
model = True # save model changes into admin's LogEntry table
fields = ('f1', 'f2') # save these fields history to AttributeLogEntry table
f1 = CharField(max_length=100)
f2 = IntegerField()
</code></pre>
<p>for threadlocals, see http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser</p>
<p>Aware! Not thoroughly tested yet. May cause problems with loading fixtures.</p>
Freely redistributableCommand to make fixtures.
2008-07-27T03:36:06.861738-05:00buriyhttps://djangosnippets.org/snippets/918/<p>"Make fixture" command. Highly useful for making test fixtures.
Use it to pick only few items from your data to serialize, restricted by primary keys.
By default command also serializes foreign keys and m2m relations.
You can turn off related items serialization with <code>--skip-related</code> option.
</p>
<p>How to use:
</p>
<pre><code>python manage.py …</code></pre>
Freely redistributableMore information about users and groups in user admin
2008-07-12T05:11:24.239326-05:00buriyhttps://djangosnippets.org/snippets/876/<p>Add-on for auth app of newforms-admin branch, adding more information about users and their groups at user and group list in admin interface. Also, nice example on customization of contributed django admin apps.</p>
<p>It adds the following to the user list interface: fields for is_superuser and is_staff, last login time; …</p>
Freely redistributableVariable inspect filter
2008-05-06T17:22:57.602613-05:00buriyhttps://djangosnippets.org/snippets/743/<p>This module has two template filters allowing you to dump any template variable. Special handling for object instances. Pretty output.
</p>
<p>Usage:
{% load dumper %}
...
<pre>{{ var|rawdump }}</pre>
or
{% load dumper %}
...
{{ var2|dump }}
</p>
<p>How to install:
As usual, put into <code><your-proj>/<any-app>/templatetags/dumper.py</code>.
</p>
Freely redistributableFixedEmailMessage
2008-03-06T05:01:03.695132-06:00buriyhttps://djangosnippets.org/snippets/630/<p>Django EmailMessage class has no cc support and has bug in bcc support.</p>
<p>Core developers won't add cc support (see ticket http://code.djangoproject.com/ticket/5790),
and I don't want to wait half a year until they will realize they have a flaw that bcc recipients are sent to regular "to:" recipients and fix …</p>
Freely redistributablehtml helpers for images and links
2007-10-09T09:12:21.599117-05:00buriyhttps://djangosnippets.org/snippets/441/<p>link and img are both HTML construction helpers.
Good idea to use these helpers if your html don't fit into templates.</p>
Freely redistributableAdminPeepingMiddleware
2007-09-11T14:00:06.585196-05:00buriyhttps://djangosnippets.org/snippets/415/<p>Peeping middleware, that replaces active user to another one
for current http request. Admin permissions required to activate,
so you can place this snippet even on the production server.
Very useful for debugging purposes. Wish it to be part of Django.</p>
<p>How to use:</p>
<p>Put this middleware after all other …</p>
Freely redistributableFieldLevelPermissionsAdmin
2007-09-11T13:52:19.775616-05:00buriyhttps://djangosnippets.org/snippets/414/<p>Have you ever needed to customize permissions, for example, allow only some fields for editing by some group of users, display some fields as read-only, and some to hide completely?
FieldLevelPermissionsAdmin class does this for newforms-admin branch.
Not tested well yet (>100 LOC!).</p>
<p>You typically would like to use it …</p>
Freely redistributable