Login

All snippets written in Python

Snippet List

Exists Filter OneToOneField in Admin

Adds Boolean like Filter in Admin to OneToOneField reference. Allowing to filter in the Parent for instances without Referenced Field. To register the filter, import in `urls.py` for example: import filterspecs Example `models.py`: from django.db import models class Place(models.Model): name = model.CharField(maxlength=50) address = model.CharField(maxlength=80) class Restaurant(meta.Model): place = model.OneToOneField(Place) place.exists_filter = True serves_hot_dogs = model.BooleanField() serves_pizza = model.BooleanField() Example `admin.py`: from django.contrib import admin from models import * class PlaceAdmin(admin.ModelAdmin): list_filter = ('restaurant',) admin.site.register(Place, PlaceAdmin) With this example PlaceAdmin will have a filter: By Restaurant All Yes No Where `Yes` will list `Place` with `Restaurant` instances, and `No` will list `Place` without `Restaurant` instances.

  • admin
  • filters
  • OneToOneField
Read More

Choice Submit Widget

This widget renders choices as submit buttons. This may be a better choice than radio buttons + submit sometimes

  • choices
  • widget
  • submit
Read More

Automatically create urls for templates in a directory

When developing a site, I sometimes want to be able to visually reference the HTML before it's given a real context, and a view. This extra bit of code will allow you to add a directory within your templates dir, and pull any html files and subdirectories from it. I added the if statement to automatically use an index.html file if there's no file and extension at the end. You can substitute anything you like for the "html" in "(?P<base>html)". That's just passed as an argument to prepend to the template as a base directory for the templates.

  • urls
  • templates
  • direct_to_template
Read More

Better slugify

Better slugify function for national characters. Source of original script: http://trac.django-fr.org/browser/site/trunk/project/links/slughifi.py?rev=47

  • unicode
  • slugify
  • slughifi
Read More

Django model objects and querysets dehydration/hydration

Dehydrates objects that can be dictionaries, lists or tuples containing django model objects or django querysets. For each of those, it creates a smaller/dehydrated version of it for saving in cache or pickling. The reverse operation is also provided so dehydrated objects can also be re-hydrated. *Example:* >>> import pickle >>> users = list(User.objects.all()[:20]) >>> print users [<User: Indiana Jones>, <User: Bilbo Baggins>, ...] >>> pickled_users = pickle.dumps(users) >>> print len(pickled_users) 17546 >>> dehydrated_users = dehydrate(users) >>> pickled_dehydrated_users = pickle.dumps(dehydrated_users) >>> rehydrated_users = hydrate(pickle.loads(pickled_dehydrated_users)) >>> print rehydrated_users [<User: Indiana Jones>, <User: Bilbo Baggins>, ...] >>> print len(pickled_dehydrated_users) 1471

  • models
  • orm
  • queryset
  • hydrate
  • dehydrate
Read More

UTC-based astimezone filter

A version of http://djangosnippets.org/snippets/2388/ which assumes UTC-based dates. This is when you store dates in UTC (as you should), and want to display them in your site's local timezone, and you notice that Django's timesince/time template tags still do not support timezones.

  • timezone
  • utc
  • timezones
  • times
Read More

Property Attributes in Memcache

Memcached Property Attributes ----------------------------- Setting the attribute will store the value in memcached; accessing the attribute will retrieve from memcached. Most useful as a way of simulating memcached "fields" on model instances. See the docstring for details.

  • cache
  • memcached
  • descriptors
Read More
Author: ori
  • 0
  • 0

ClearableFileInput with image preview

This widget allows you to display preview images with adjustable width and length of the link: [example](http://img526.imageshack.us/img526/6588/screenshotat20111026215.png) AdvancedFileInput(preview=True, image_width=200) For other files, you can adjust the length of the link without preview: [example](http://img845.imageshack.us/img845/6588/screenshotat20111026215.png) AdvancedFileInput(preview=False, url_length=30) by default, parameters are: preview = True url_length = 30 image_width = 200

  • imagefield
  • preview
  • imagepreview
  • previews-on-imagefield
Read More

Disable ordering in the admin for a model

Django admin orders models by their primary key by default, which can be undesirable on very large tables. This shows how to disable any ordering on a model. Note that this behavior is fixed in 1.4 trunk.

  • admin
  • ordering
Read More

Remove old fields on dumpdata generated json

A small script that takes a manage.py dumpdata generated json file, and removes fields of the specified models. I needed this because i kept my initial data on a json file and after I removed a field on one of my models, the script wouldn't work anymore.

  • json
  • loaddata
  • fixture
  • dumpdata
  • migrate
  • removed field
Read More

Get Client IP Behind Proxy

If your application server is behind a proxy, `request.META["REMOTE_ADDR"]` will likely return the proxy server's IP, not the client's IP. The proxy server will usually provide the client's IP in the `HTTP_X_FORWARDED_FOR` header. This util function checks both headers. I use it behind Amazon's Elastic Load Balancer (ELB).

  • request
  • ip
Read More

Persistent Session Debugging with Django Debug Toolbar

When using [django debug toolbar](https://github.com/django-debug-toolbar/django-debug-toolbar), I like to be able to turn debugging on and off without having to edit my settings file. This callback makes that possible. Add `?debug=on` to the URL to turn debugging on. It will remain on in the current session until you turn it off with `?debug=off`. Make sure your session middleware comes before your debug toolbar middleware.

  • session
  • debug
  • debug-toolbar
Read More

2956 snippets posted so far.