Login

Snippets by dcwatson

Snippet List

Unique field inline formset

This method will return an inline formset class that validates values across the given field are unique among all forms. For instance: ApprovedUserFormSet = inlineformset_factory(Request, ApprovedUser, formset=unique_field_formset('email'), form=ApprovedUserForm) Will make sure all ApprovedUser objects created for the Request have unique "email" fields.

  • field
  • unique
  • formset
  • inlineformset
Read More

Database Routing by URL

An example of how to select the "default" database based on the request URL instead of the model. The basic idea is that the middleware `process_view` (or `process_request`) function sets some context from the URL into thread local storage, and `process_response` deletes it. In between, any database operation will call the router, which checks for this context and returns an appropriate database alias. In this snippet, it's assumed that any view in the system with a `cfg` keyword argument passed to it from the urlconf may be routed to a separate database. Take this urlconf for example: `url( r'^(?P<cfg>\w+)/account/$', 'views.account' )` The middleware and router will select a database whose alias is `<cfg>`, or "default" if none is listed in `settings.DATABASES`, all completely transparent to the view itself.

  • multidb database router url
Read More

GzipFileSystemStorage

`GzipFileSystemStorage` is a `FileSystemStorage` subclass that transparently compresses files. [More Info](http://theidioteque.net/blog/2009/9/29/gzipfilesystemstorage/)

  • compression
  • file
  • gzip
  • storage
  • compress
Read More

Full-Text Searchable Models

A drop-in module to allow for full-text searchable models with very little effort. Tested with PostgreSQL 8.3, but should work on earlier versions with the tsearch2 module installed.

  • models
  • search
  • full-text
  • tsearch2
Read More

dcwatson has posted 4 snippets.