Login

All snippets

Snippet List

autotranslate po files using google translate

Save to autotranslate.py, run using python autotranslate.py pofile inputlang outputlang, eg. python autotranslate.py path_to_blank_fr_lang.po en fr, to translate to french. Some known bugs: * Doesn't handle some line returns properly * Block translations aren't formated correctly in the translation. If anyone has any issues or fixes please post to the comments. Of course the output shouldn't be used as substitute to a proper translation.

  • translation
Read More

WorldIP - access to IP database over API

The WorldIP database provides real-world geographical location. Database is more correct than [Whois records and Whois-based databases](http://www.wipmania.com/en/blog/why-worldip-data-rather-than-whois-data-examples/), that show geographic locations of network owners, and not the geographic location of Internet-connected PC or appliance itself. See more: [WIPmania.com](http://www.wipmania.com)

  • ip
  • geolocation
  • geodjango
  • worldip
Read More

local django model test

Sometimes you need to test some model features without a complete django app installation. Just play only with the model object. With these small script you have a complete in memory django installation. Some Links: http://www.djangosnippets.org/snippets/1044/ (en) http://www.jensdiemer.de/permalink/150/mein-blog/99/django-db-model-test/ (de) http://www.python-forum.de/viewtopic.php?f=3&t=15649 (de) See also: https://github.com/readevalprint/mini-django/

  • model
  • testing
  • test
  • local-test
Read More

Bind Administration

Binds $Model to $ModelAdmin without having to specify each binding manually. The ModelAdmins **must** have the same name as the model (as well as same case) with Admin appended. Example: from django.db import models class SomeModel(models.Model): name = models.CharField(max_length=255) class AnotherModel(models.Model): name = models.CharField(max_length=255) # bind administration bind_administration('myproject.myapp.models', 'myproject.myapp.admin')

  • admin
  • 1.0
  • modeladmin
Read More

Improved Accept header middleware

A clean and simple implementation of parsing the Accept header. It places the result in request.accepted_types. Place this middleware anywhere in the chain, as all it does is add to the request object.

  • middleware
  • accept
  • header
Read More

Decorator cache handler per view

The @cache_holding decorator works in a per-function base, you can specify a list of models or just a model and the second argument is the time in seconds to be cached. You can modify the proxy class by a keyword argument so you can do cache to all kind of things, also if you want to use a prefix as key + the hash value you can specify the prefix keyword argument.

  • cache
  • view
  • decorator
Read More

Copy a model instance

Create a copy of a model instance. Works in model inheritance case where ``instance.pk = None`` is not good enough, since the subclass instance refers to the parent_link's primary key during save. M2M relationships are currently not handled, i.e. they are not copied. See also Django #4027.

  • model
  • copy
Read More

Remove quotes from bits in a custom tag

A simple function that will remove the quote marks from every string in a list - if and only if the quote marks are the first and last character of the string, regardless of whether they are single or double quotes. Useful when parsing the arguments of a custom tag.

  • quotes
  • parse
  • custom-tags
  • tokens
Read More

server with debugging backdoor

this starts up two servers - HTTP serving the django application on port 8000 and a port 8001 server that will start an interactive interpreter with any incoming connections. this enables you to have an interpreter in the same process as your server. $ wget http://localhost:8000/someurl/ (...) $ nc localhost 8001 Python 2.5.2 (r252:60911, Jul 8 2008, 21:21:10) [GCC 4.3.1 20080626 (prerelease)] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from django.db import connection >>> connection.queries [ ... ]

  • debugging
Read More

ParentModel and ChildManager for Model Inheritance

This is the approach I've taken to access instances of child models from their parent. Functionally it's very similar to snippets [1031](http://www.djangosnippets.org/snippets/1031/) and [1034](http://www.djangosnippets.org/snippets/1034/), but without the use of `django.contrib.contenttypes`. Usage: class Post(ParentModel): title = models.CharField(max_length=50) objects = models.Manager() children = ChildManager() def __unicode__(self): return self.title def get_parent_model(self): return Post class Article(Post): text = models.TextField() class Photo(Post): image = models.ImageField(upload_to='photos/') class Link(Post): url = models.URLField() In this case, the `Post.children` manager will return a queryset containing instances of the appropriate child model, rather than instances of `Post`. >>> Post.objects.all() [<Post: Django>, <Post: Make a Tumblelog>, <Post: Self Portrait>] >>> Post.children.all() [<Link: Django>, <Article: Make a Tumblelog>, <Photo: Self Portrait>]

  • model
  • manager
  • queryset
  • inheritance
Read More

Easy file upload handler

This function emulates the file upload behaviour of django's admin, but can be used in any view. It takes a list of POST keys that represent uploaded files, and saves the files into a date-formatted directory in the same manner as a `FileField`'s `upload_to` argument.

  • image
  • forms
  • view
  • upload
  • imagefield
  • filefield
  • file
Read More

Overriding Third-party Admin

With the advent of newforms-admin it's now possible to override admin interfaces without having to change any code in third-party modules. This example shows how to enable a rich-text editor for `django.contrib.flatpages` without touching Django's code at all. (Actual embedding of the editor via Javascript left as an exercise for the reader – plenty of examples of that elsewhere.)

  • admin
  • newforms-admin
  • custom
  • contrib.admin
  • override
  • rich-text
Read More

Model inheritance with content type and inheritance-aware manager

inspired by crucialfelix's [inheritance hack](http://www.djangosnippets.org/snippets/1031/), which was a far better method of fetching a model's subclassed version from an instance than my own, i decided to bake his snippet in to my own inheritance hack, which i think benefits both. the result is a query set that returns subclassed instances per default. So - in the snippet's example, querying `Meal.objects.all()[0]` will return a salad object, if that instance of Meal happens to be the parent of a Salad instance. To my mind this is closer to the 'intuitive' way for a query set on an inheriting model to behave. **Updated:** added subclassing behaviour for the iterator access as well. **Updated:** incorporated carljm's feedback on inheritance

  • manager
  • inheritance
Read More

3110 snippets posted so far.