djangosnippets.org: Latest snippets posted by gsakkishttps://djangosnippets.org/users/gsakkis/2010-10-03T10:01:25.101574-05:00gsakkisPolymorphic inheritance ala SQLAlchemy
2010-10-03T10:01:25.101574-05:00gsakkishttps://djangosnippets.org/snippets/2216/<p>This is a different take on polymorphic inheritance, inspired by SQLAlchemy's approach to the problem.</p>
<p>The common Django approach (e.g. snippets 1031 & 1034, <a href="http://github.com/bconstantin/django_polymorphic">django_polymorphic</a>) is to use a foreign key to <code>ContentType</code> on the parent model and override <code>save()</code> to set the right content type automatically. That works …</p>
Freely redistributableAccess transparently the user profile information
2010-09-04T11:08:44.698461-05:00gsakkishttps://djangosnippets.org/snippets/2187/<p>Splitting the information about a user across two models (User and UserProfile) is not to everyone's liking. This snippet monkeypatches the User model so that users can access transparently their profile information while still storing the data in two tables under the hood. Thus it is similar to the inheritance …</p>
Freely redistributableEasy configuration for relocatable sites
2010-08-06T04:41:02.973161-05:00gsakkishttps://djangosnippets.org/snippets/2136/<p>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 <code>settings.py</code>.
</p>
<p>This allows deployment under a different mount point with a single Django setting, without having to …</p>
Freely redistributableArbitrary auto-generated primary keys
2010-07-23T11:23:53.045296-05:00gsakkishttps://djangosnippets.org/snippets/2122/<p>Auto-incremented primary keys are the default in Django and they are supported natively in most databases but for anything more complex things are less trivial. DB sequences are not standard, may not be available and even if they are, they are typically limited to simple integer sequence generators. This snippet …</p>
Freely redistributableInternal view decorator
2010-07-22T09:21:59.718453-05:00gsakkishttps://djangosnippets.org/snippets/2120/<p>The <code>internal_view</code> decorator makes a view accessible only from <code>INTERNAL_IPS</code>. Handy for exposing internal APIs.</p>
Freely redistributableEasier custom Model Manager Chaining
2010-07-20T08:02:36.625552-05:00gsakkishttps://djangosnippets.org/snippets/2117/<p>A zero-boilerplate generalisation of http://djangosnippets.org/snippets/2114/</p>
Freely redistributableWhile loop template tag
2010-07-05T16:00:27.840870-05:00gsakkishttps://djangosnippets.org/snippets/2096/<p>The missing <code>while</code> template tag. Built on top of http://djangosnippets.org/snippets/2093/, it also supports <code>break</code> and <code>continue</code> out of the box.</p>
Freely redistributableContinuing and breaking from loops in Django templates
2010-07-04T14:28:44.717080-05:00gsakkishttps://djangosnippets.org/snippets/2093/<p>This snippet makes Django templates support <code>break</code> and <code>continue</code> in loops. It is actually more powerful than the respective Python statements as it allows breaking and continuing from an outer loop, not just the innermost.</p>
<p><code>break</code> and <code>continue</code> are implemented as template filters, with the input value being the loop …</p>
Freely redistributableManyToManyField with maximum cardinality constraints
2010-06-20T05:54:49.020807-05:00gsakkishttps://djangosnippets.org/snippets/2073/<p>An extension of ManyToManyField for limiting the maximum number of relationships. See the docstring for more information and sample usage.</p>
Freely redistributableExtended cacheable callables and properties
2010-06-06T19:09:04.619486-05:00gsakkishttps://djangosnippets.org/snippets/2053/<p>There are several snippets that provide a basic caching decorator for functions and methods (e.g. #202, #1130, etc.). The <code>Cacheable</code> class in this snippet extends them by (<em>if you don't see an unordered list below, the Markdown on this site is still broken...</em>):
</p>
<ul>
<li>
Specifying how cache keys are …</li></ul>
Freely redistributableEasier prefix handling for forms
2010-04-17T08:56:25.598275-05:00gsakkishttps://djangosnippets.org/snippets/1992/<p><strong>autoprefixed</strong> is a decorator for Form classes that simplifies prefix handling by storing it in a hidden field. Thus when the form is posted, the prefix can be extracted from the posted data instead of having to pass it explicitly when instantiating the form.</p>
Freely redistributableBreaking tests.py into multiple files
2010-04-01T12:31:41.778267-05:00gsakkishttps://djangosnippets.org/snippets/1972/<p>Django loads tests found in models.py and tests.py (if present) or actually a module or package named 'tests' under the app. Since tests can be a package, one can create a 'tests' directory, split the test cases across multiple files under 'tests' and import them from tests/<strong>init</strong>.py with: …</p>
Freely redistributableregister.tag as a class decorator
2010-01-27T19:24:22.605147-06:00gsakkishttps://djangosnippets.org/snippets/1894/<p>Defining a custom template tag consists of three parts: a compiling function, a rendering <code>Node</code> subclass and a tag registration with <code>register.tag()</code>. The latter can be used as a (function) decorator on the compiling function, simplifying things into two parts.</p>
<p>A neat fact is that <code>register.tag()</code> can actually be …</p>
Freely redistributable