djangosnippets.org: Latest snippets posted by guettlihttps://djangosnippets.org/users/guettli/2012-02-22T06:58:53.558080-06:00guettliupdate primary key (and cascade to child tables)
2012-02-22T06:58:53.558080-06:00guettlihttps://djangosnippets.org/snippets/2691/<p>Management command to update a primary key and update all child-tables with a foreign key to this table.</p>
Freely redistributableClean Reversion History: Remove unimportant Changes
2012-02-03T05:57:17.104173-06:00guettlihttps://djangosnippets.org/snippets/2672/<p>If you use this pattern to track changes in the auth user table:</p>
<pre><code>from django.contrib.auth.models import User
from reversion.helpers import patch_admin
patch_admin(User)
</code></pre>
<p>you can't see important changes, since a version is created for every login if a user. If you want to get rid of changes which only change unimportant …</p>
Freely redistributableReadOnlyWidget
2009-08-13T07:20:06.000639-05:00guettlihttps://djangosnippets.org/snippets/1682/<p>See docstring</p>
Freely redistributableImportObject: Import model *instance* specific code at runtime
2009-08-13T02:00:02.772788-05:00guettlihttps://djangosnippets.org/snippets/1681/<p>See docstring</p>
Freely redistributableStringListField (database field)
2009-05-06T01:47:04.481196-05:00guettlihttps://djangosnippets.org/snippets/1491/<p>This django.db.models.Field stores a list of python strings in one database column.</p>
Freely redistributableCookie based Messages (deprecated)
2009-04-22T05:45:32.839244-05:00guettlihttps://djangosnippets.org/snippets/1459/<p>DEPRECATED: Django has cookie based messages since version 1.2</p>
<p>This messages stores messages for the user in a cookie. I prefer this to session based messages, because the session column is a hot spot in the database if one user
works with several browser tabs/windows.</p>
Freely redistributableOverwrite some views in settings.py
2009-03-30T10:13:13.417447-05:00guettlihttps://djangosnippets.org/snippets/1404/<p>If you app defines some URLs with a name, and you want to overwrite this
at project level with a different view you can use this snippet. You only need
to change on line in the application code (the import statement).</p>
Freely redistributabledecoupling authorization from calling the view
2008-11-25T01:20:00.572425-06:00guettlihttps://djangosnippets.org/snippets/1214/<p>has_perm.py: Check if a user can access a view without calling it</p>
<p>One of the goals:
You can disable or hide a link if the user must not call it. </p>
Freely redistributableDatabase Schema Upgrade (deprecated)
2008-10-30T04:34:00.184436-05:00guettlihttps://djangosnippets.org/snippets/1160/<p>Deprecated! I use south in all applications with database backend now.</p>
Freely redistributableurlquote() and urlencode() in one method
2008-10-30T03:02:12.433817-05:00guettlihttps://djangosnippets.org/snippets/1159/<p>I think this method is handy, since you don't need to remember if you
need urlquote or urlencode. It does both and adds a question mark
between the path and the get parameters, if later are present.</p>
<p>And it works around a bug in Django: MultiValueDicts (request.GET, request.POST) are handled …</p>
Freely redistributableAdd ValidationError to a field instead of __all__ during Form.clean()
2008-10-13T09:56:15.521116-05:00guettlihttps://djangosnippets.org/snippets/1132/<p>If you need two values for validation, you can't use clean_value_a() since it is undefined
if value_a or value_b gets cleaned first.</p>
<p>This little helper lets you add ValidationErrors to fields instead of _<em>all_</em>.</p>
<p>Related <a href="http://code.djangoproject.com/ticket/5335">Ticket 5335</a>.</p>
Freely redistributableTimedelta Database Field
2008-09-17T08:05:49.027943-05:00guettlihttps://djangosnippets.org/snippets/1060/<p>This subclass of django.models.Field stores a python datetime.timedelta object
as an integer column in the database.</p>
<p>It includes a TimedeltaFormField for editing it through djangos admin interface.</p>
<p>Feedback welcome.</p>
<p>2011-04-20: TimedeltaField can now be (de)serialized. Tested with JSON.</p>
<p>2009-11-23: <code>_has_changed()</code> added. Before form.changed_data contained timedelta FormFields,
even if nothing changed.</p>
Freely redistributablebreadcrumbs
2008-09-05T07:46:42.944303-05:00guettlihttps://djangosnippets.org/snippets/1026/<p>With this script you can create a simple breadcrumbs line for navigation: Home >> Page >> Subpage ...</p>
Freely redistributableUnittest: Compare max_size with column size of database
2008-08-18T08:25:22.564520-05:00guettlihttps://djangosnippets.org/snippets/990/<p>Changing the size of max_length in the model is fast. But sometimes you forget to
update all running systems which use this model.</p>
<p>This unittest helps you to find the difference between Model and DB before the users get uncaught exceptions.</p>
Freely redistributableAdd HTML Attributes in Model
2008-05-15T05:43:00.783674-05:00guettlihttps://djangosnippets.org/snippets/755/<p>Maybe there is a better solution, feedback welcome!</p>
Freely redistributable