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.
[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.
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.
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.
all_models = [ each for each in GetAllModels() ]
This produces a list of tuples in format ( app_label's name, model's name) of all the ContentType existing in system.
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
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.
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.
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.
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'
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.