djangosnippets.org: Latest snippets posted by divermanhttps://djangosnippets.org/users/diverman/2012-01-10T16:21:06.401532-06:00divermanJSON instead of pickle for memcached
2012-01-10T16:21:06.401532-06:00divermanhttps://djangosnippets.org/snippets/2642/<p>Standard memcache client uses pickle as a serialization format. It can be handy to use json, especially when another component (e.g. backend) doesn't know pickle, but json yes.</p>
Freely redistributableHow to rename processes
2011-10-23T06:33:12.085629-05:00divermanhttps://djangosnippets.org/snippets/2577/<p>Rename apache (or any) processes to be easily identified from ps output.</p>
Freely redistributableOther approach of making middleware (by decorators)
2011-03-06T12:40:27.569218-06:00divermanhttps://djangosnippets.org/snippets/2383/<p>Other approach of making middleware. Advantage of is to specify, which middleware is used for which view function and in what order. Middleware function gets all arguments, that are passed to view function.
</p>
<p><strong>Example usage</strong>
</p>
<pre><code>@RequestMiddleware
def print_params_middleware(request, *args, **kwargs):
print 'GET params:', request.GET
@ResponseMiddleware
def modify_headers_middleware(request, response, *args, **kwargs): …</code></pre>
Freely redistributableDoing redirect without request
2010-09-23T08:55:10.918970-05:00divermanhttps://djangosnippets.org/snippets/2204/<p>When you neeed to do redirect and request object is not available, you can do it with exception.</p>
<p>Put exception handler somewhere request is available, for example to middleware or ModelAdmin.</p>
<p>Raise exception, where request is not available.</p>
Freely redistributablePostgreSQL fulltext with language translations
2010-08-15T10:37:13.874601-05:00divermanhttps://djangosnippets.org/snippets/2148/<p>Consider following models:
</p>
<pre><code>class Product(models.Model):
code = modeld.CharField()
class ProductTrans(models.Model):
product = models.ForeignKey('Product')
language = models.ChoiceField(choices=settings.LANGUAGES)
title = models.ChaField()
description = models.ChaField()
</code></pre><p>With this snippet is possible search through all translations of product at the same time (using string concatenation in trigger):
</p>
<pre><code>Product.objects.extra(
where = ['product_product.fulltext @@ to_tsquery(%s)'],
params = …</code></pre>
Freely redistributableUsing URLs for highlighting sections in menu
2010-08-09T17:15:18.659869-05:00divermanhttps://djangosnippets.org/snippets/2143/<p>Use this, if you want to "activate" menu items by URL. Typical usage is with CSS class 'active'.</p>
Freely redistributableQuery lookups using operators
2010-08-04T16:09:10.326913-05:00divermanhttps://djangosnippets.org/snippets/2135/<p>This class emulates query lookups to behave as numeric operators. Inspired by SQLAlchemy.
</p>
<pre><code>User.objects.filter( X('username') == 'diverman' )
User.objects.filter( X('username') != 'diverman' )
User.objects.filter( X('pk') > 10 )
User.objects.filter( X('pk') >= 10 )
User.objects.filter( X('pk') < 10 )
User.objects.filter( X('pk') <= 10 )
User.objects.filter( X('username') % 'iverma' )
User.objects.filter( X('username') …</code></pre>
Freely redistributableadding fields to User model
2010-08-02T17:09:37.372566-05:00divermanhttps://djangosnippets.org/snippets/2132/<p>This code adds new field to Django user model. It must be executed early as much as possible, so put this code to <strong>init</strong>.py of some application.</p>
Freely redistributableNoneWidget
2010-06-30T13:54:29.376387-05:00divermanhttps://djangosnippets.org/snippets/2084/<p>Sometime may be useful to disable the HTML output from formfield to template.</p>
Freely redistributableIn-memory XML-RPC server based on URL
2010-06-23T16:21:09.825137-05:00divermanhttps://djangosnippets.org/snippets/2078/<p>This is a XML-RPC server, that uses arguments in URLs and every dispatcher instance is prepared in memory during webserver run. It's good, for example, for securing XML-RPC server with hashed strings and there are a lot of similar use cases.</p>
<p>Usage:</p>
<pre><code>from xmlrpclib import ServerProxy
server = ServerProxy('http://example.com/xmlr-rpc/%s/' % …</code></pre>
Freely redistributableFieldStack - easy form template rendering
2010-05-26T16:53:42.284344-05:00divermanhttps://djangosnippets.org/snippets/2040/<p>FieldStack simplifies forms template rendering. This is enhanced version of snippet <a href="http://djangosnippets.org/snippets/1786/">1786</a></p>
Freely redistributableTrueNoneField
2009-12-08T16:06:43.239323-06:00divermanhttps://djangosnippets.org/snippets/1831/<p>This custom model field is a variant of NullBooleanField, that stores only True and None (NULL) values. False is stored as NULL.</p>
<p>It's usefull for special purposes like unique/unique_together.</p>
<p>One small problem is here, that False is not lookuped as None.</p>
<p>This snippets is a response to <a href="http://www.djangosnippets.org/snippets/1830/">1830</a></p>
Freely redistributable{% eval %} templatetag-evaluate expressions
2009-11-27T20:17:23.588101-06:00divermanhttps://djangosnippets.org/snippets/1820/<p>This templatetag evaluates Python expressions including built-in functions, lambda functions etc... Use with caution.</p>
Freely redistributableStacked/Grouped Forms 2 - easy rendering forms
2009-11-09T17:32:04.586247-06:00divermanhttps://djangosnippets.org/snippets/1786/<p>This snippet was inspired by <a href="http://www.djangosnippets.org/snippets/1783/">1783</a>. It allows simply create groups of fields for template rendering.</p>
Freely redistributableURL models
2009-10-09T18:11:07.721923-05:00divermanhttps://djangosnippets.org/snippets/1755/<p>You can use <code>UrlModel</code> to provide URL functionality to any instance of any model and any language (language support can be removed from this). Each model must have own view method, that returns HttpResponse. I was inspired by Flatpages. It is useful for small sites and static pages.</p>
<pre><code>`class Page(UrlModel): …</code></pre>
Freely redistributable