djangosnippets.org: Latest snippets posted by carljmhttps://djangosnippets.org/users/carljm/2009-02-08T08:48:08.141517-06:00carljmBetterForm with fieldsets and row_attrs
2009-02-08T08:48:08.141517-06:00carljmhttps://djangosnippets.org/snippets/1314/<p><strong>NOTE</strong>: Further development of this snippet will take place in the <a href="http://launchpad.net/django-form-utils">django-form-utils</a> project.</p>
<p>This snippet provides BetterForm and BetterModelForm classes which are subclasses of django.forms.Form and django.forms.ModelForm, respectively. BetterForm and BetterModelForm allow subdivision of forms into fieldsets which are iterable from a template, and also allow definition of row_attrs …</p>
Freely redistributableimproved generic foreign key manager
2008-09-22T16:03:06.179379-05:00carljmhttps://djangosnippets.org/snippets/1079/<p>This is an improvement on <a href="http://www.djangosnippets.org/snippets/984/">snippet 984</a>. Read it's description and <a href="http://zerokspot.com/weblog/2008/08/13/genericforeignkeys-with-less-queries/">this blog post</a> for good explanations of the problem this solves.
</p>
<p>Unlike snippet 984, this version is able to handle multiple generic foreign keys, generic foreign keys with nonstandard ct_field and fk_field names, and avoids unnecessary lookups to …</p>
Freely redistributableTestSettingsManager: temporarily change settings for tests
2008-08-30T01:48:51.725617-05:00carljmhttps://djangosnippets.org/snippets/1011/<p>This TestSettingsManager class takes some of the pain out of making temporary changes to settings for the purposes of a unittest or doctest. It will keep track of the original settings and let you easily revert them back when you're done. </p>
<p>It also handles re-syncing the DB if you modify …</p>
Freely redistributableobject-oriented generic views
2008-08-28T22:49:36.446084-05:00carljmhttps://djangosnippets.org/snippets/1009/<p>Here's an example of writing generic views in an object-oriented style, which allows for very fine-grained customization via subclassing. The snippet includes generic create and update views which are backwards compatible with Django's versions.
</p>
<p>To use one of these generic views, it should be wrapped in a function that creates …</p>
Freely redistributableModelForm-based create_update generic views
2008-07-15T21:30:39.535442-05:00carljmhttps://djangosnippets.org/snippets/885/<p>This is a ModelForms-based rewrite of the create_object and update_object generic views, with a few added features. The views now accept a "form_class" argument optionally in place of the "model" argument, so you can create and tweak your own ModelForm to pass in. They also accept a "pre_save" callback that …</p>
Freely redistributableMarkdownTextField
2008-07-14T16:43:02.695075-05:00carljmhttps://djangosnippets.org/snippets/882/<p>A <a href="http://code.djangoproject.com/wiki/UsingMarkup">common pattern in Django</a> is to create a TextField intended for Markdown text (i.e. description) and a companion non-editable TextField for storing the HTML version (i.e. description_html), so the Markdown converter need not be run for every page view.<br />
</p>
<p>This snippet is a custom field which encapsulates this pattern …</p>
Freely redistributableYAAS (Yet Another Auto Slug)
2008-05-22T19:03:20.845796-05:00carljmhttps://djangosnippets.org/snippets/770/<p>This is the self-populating AutoSlugField I use. It's not the <a href="http://www.djangosnippets.org/tags/slug/">first such snippet</a>, but (IMO) it works a bit more cleanly. It numbers duplicate slugs (to avoid IntegrityErrors on a unique slug field) using an "ask-forgiveness-not-permission" model, which avoids extra queries at each save. And it's simply a custom …</p>
Freely redistributable