Login

Top-rated snippets

Snippet List

Generic Model

In this type of model you are allowed to define a model with a generic type. For instance, a location can be an address, GPS coordinates, an intersection and many others types. Using a many to many field, models can have multiple locations without worrying about the type of location referencing. New locations types can be added without changing the references in other models. This code is also used in Django's built in ContentTypes app.

  • models
  • generic
Read More

more on manager methods

[Snippet #2](http://www.djangosnippets.org/snippets/2/) demonstrated some cool tricks possible with manager methods. This example shows how to assign and use a custom manager method. In this snippet the `belongs_to_user` method returns an Account queryset containing only those accounts associated with the specified user. The method is useful because it hides the implementation of User in the Account model. Line 17 associates the custom manager with the Account model.

  • managers
Read More

Serving null files

Sometimes you have to serve null files. This quick hacky generic view lets you do that. The best example of that is robots.txt, I want my robots.txt to always be empty, at least for now. The other example would be favicon.ico, but that's more often used to actually fill a purpose.

  • null
  • files
  • views
Read More

Form and ModelForm inheritance DRY

Modelform cant inhertit from forms. To solve this issue, split thing you wanto to inherit into filed definition and functionality definition. For modelform use the base_fields.update method as mentioned in the code.

  • form
  • inheritance
Read More

Automatically set up a project

This is a script to automatically set up a django project It takes only one argument for the project name This works for Django 1.4 It will create the following directory structure: /project /server (app name) /media: /html /css /js /img

  • automatic
  • createproject
Read More

Prefetch id for Postgresql backend

When uploading a file or image, you need to put it somewhere that's not going to be orphaned by a change in the model. You could use a globally unique value like a uuid, but the django id autofield is a much shorter surrogate field that can be used in a friendly path to the object. The problem with id autofields is that they don't exist when the object is created for the first time - so all files using the id get uploaded to a location 'None' on first save, increasing the likelihood of name collisions. This postgresql only snippet fetches the next id in a way that is safe for concurrent access. This snippet only works on postgresql because neither sqlite or mysql have a nextval equivalent. Instead you would have to lock the table for writes and select the last value inserted incrementing it yourself.

  • models
  • filefield
Read More

haystack auto update (forget about update_index)

Dead simple snippet. Paste it in some models (i use project_specific/models.py), and you don't need to run update_index anymore. When a model is deleted from the database: it is deleted from the index. When a model is saved (created or modified): it is updated in the index.

  • haystack
Read More

Dynamical formsets

If you need dynamical formsets you can use `{{ empty_form }}` in template and even makeup it.

  • formset
Read More

Replacing pattern groups by values

This function takes a pattern with groups and replaces them with the given args and/or kwargs. Example: IMPORTANT: this code is NOT to use replacing Django's reverse function. The example below is just to illustrate how it works. For a given pattern '/docs/(\d+)/rev/(\w+)/', args=(123,'abc') and kwargs={}, returns '/docs/123/rev/abc/'. For '/docs/(?P<id>\d+)/rev/(?P<rev>\w+)/', args=() and kwargs={'rev':'abc', 'id':123}, returns '/docs/123/rev/abc/' as well. When both args and kwargs are given, raises a ValueError.

  • regex
  • reverse
Read More

User Profile minimal code

Usually I start an authentication app with this model. Don't forget to set it up in the settings file AUTH_PROFILE_MODULE = 'authentication.UserProfile'

  • user
  • profile
  • signals
  • get_profile
Read More

3110 snippets posted so far.