Login

All snippets written in Python

Snippet List

A ModelChoiceField with support for title in options based on a field in the model

ModelChoiceTitleField is a ModelChoiceField descendent that creates <OPTIONS> with title elements based on the field specified in title_source_field: priority=ModelChoiceTitleField(Priority.objects.all(), initial=Priority.objects.get(default=True).id, title_source_field='long_description') That will generate a `<SELECT>` element looking like: <select name="priority" id="id_priority"> <option value="1" title="Some extremely important task.">Emergency</option> ... </select> In the `<option>` above, the title was retrieved from the `long_description` field for the instance of the Priority class. The word Emergency came from a call to the instance of the Priority class' `__unicode__()` method. The value of the option is the PK for the instance of the Priority class.

  • ModelChoiceField
Read More

Return to a filtered changelist on change form save

You've filtered your changelist in your admin site and you want to edit a few entries here; you click on an object, edit it and press save, and you end up back at the default unfiltered changelist view. This ModelAdmin override is so that if you press "save" (not "save and add another", or "save and continue editing") you end up back at your filtered changelist. There are other ways out there and other snippets to do similar; however I hadn't seen one to only redirect if you pressed save so this is what I came up with. Hopefully it's useful.

  • admin
  • redirect
Read More

Easy configuration for relocatable sites

Deploying relocatable Django sites isn't currently as trivial as it should be (see http://code.djangoproject.com/ticket/8906, http://groups.google.com/group/django-developers/tree/browse_frm/thread/fa3661888716f940/). This snippet relocates all url patterns (similarly to http://djangosnippets.org/snippets/2129/) as well as the absolute url settings of `settings.py`. This allows deployment under a different mount point with a single Django setting, without having to repeat the mount point again as a SCRIPT_NAME parameter supplied by the web server.

  • deployment
  • mount point
  • relocatable
Read More

Query lookups using operators

This class emulates query lookups to behave as numeric operators. Inspired by SQLAlchemy. User.objects.filter( X('username') == 'diverman' ) User.objects.filter( X('username') != 'diverman' ) User.objects.filter( X('pk') > 10 ) User.objects.filter( X('pk') >= 10 ) User.objects.filter( X('pk') < 10 ) User.objects.filter( X('pk') <= 10 ) User.objects.filter( X('username') % 'iverma' ) User.objects.filter( X('username') << 'diver' ) User.objects.filter( X('username') >> 'man' ) User.objects.filter( X('pk') | (1, 2, 3) )

  • q
  • query
  • lookup
  • operator
Read More

Reset / Send account details email

I needed to create a feature for administrators to create accounts and email the *new account was created* email using a button. Remember to change the default template and subject to match your needs. The code is directly taken from `django.contrib.auth.forms.PasswordResetForm.save()`. Notice that the function raises `ValueError` if the user is missing an email address.

  • users
  • reset-password
Read More

adding fields to User model

This code adds new field to Django user model. It must be executed early as much as possible, so put this code to __init__.py of some application.

  • fields
  • model
  • user
  • auth
  • field
Read More

Filter to generate QR codes

You can convert any string to a QR code image as easy as use a simple filter, thanks to google charts api. Common use: <img src="{{object.attribute_to_encode|qr:"120x130"}}" /> This will create a 120(width) x 130(heiht) image with the value of the attribute_to_encode encoded in a QR coded image.

  • filter
  • templatetag
  • templatefilter
  • barcode
  • qr
Read More

Use both NTLM and Anonymous authentication with IIS

Point '^accounts/login/$' or whatever your custom login path is to the 'negotiate_ntlm' view. This allows you to keep anonymous authentication enabled on IIS and easily lock down just the parts of the site you need to (e.g. [admin](http://djangosnippets.org/snippets/2127/)).

  • admin
  • authentication
  • anonymous
  • iis
  • ntlm
Read More

Interactive Profiling Middleware

Based on [Extended Profiling Middleware](http://djangosnippets.org/snippets/605/), this version allows interactive sorting of functions and inspection of SQL queries.

  • middleware
  • profile
  • hotshot
Read More

Database cleanup

***About*** I tried to dump data from my database (manage.py dumpdata) and I couldn't do it because of error: User matching query does not exists I found out that my database was filled with garbage: entries those foreigners were deleted. My table's engine is MyISAM so it allows for these lost entries to exist. I had to cleanup my database before I do datadump, so I've written a script which worked fine for me. ***Usage*** Place this script in same directory with your settings.py and run it: `python db_cleanup.py` ***Disclaimer*** Backup your data :)

  • dumpdata
Read More

2955 snippets posted so far.