Login

All snippets written in Python

Snippet List

Django Number Input

This will give you a <input type='number'> field. This is helpful when you want to use HTML5 for newer browsers. Older browsers will just interpret this as <input type='text'>

  • django
  • django-forms
  • input-types
Read More

Decoupling models with cross-database relations

The snippet enables decoupling model classes, associated with a ForeignKey, for the purpose of separating them into two databases. Looking at the following example: class Reporter(models.Model): ... class Article(models.Model): reporter = models.ForeignKey(Reporter) We want to separate the `Reporter` and `Article` into two separate databases, but this won't work in Django because of the [cross model relations](http://docs.djangoproject.com/en/dev/topics/db/multi-db/#cross-database-relations). The solution is to use an ID field and not a `ForeignKey` one. This makes the access very uncomfortable. The above class will make this a bit less awkward. It doesn't support the [RelatedManager](http://docs.djangoproject.com/en/dev/ref/models/relations/#django.db.models.fields.related.RelatedManager), but it will support accessing the related field as you normally would. The article class will look like this (assuming the reporter model id field is an `IntegerField`): class Article(DecoupledModel): reporter_id = models.IntegerField() _linked_fields = { 'reporter': Reporter, } Once you have an article object, you can access the reporter as you normally would for both read and writing. For example: my_reporter = article1.reporter article2.reporter = my_reporter

  • django
  • model
  • database
Read More

See what depends on a given model

Riffing on http://djangosnippets.org/snippets/1024/ I didn't want a graph; I just wanted to see what depended on a specific model. This does that nicely.

  • model
  • orm
  • dependency
Read More

Template Tag for Retrieving complex Settings

Very much like the snippet joshua wrote (http://djangosnippets.org/snippets/67/) for the same purpose but with the addition that this lets the user get values for more complex settings like dicts and lists

  • settings
  • template tag
Read More

Template Tag of Django Image Thumb Creator

**You can save these codes into a templatetag file in your django app. Then use codes like these in your template files:** **************************************************************************** {% load *yourtags* %} ... <img src="{% thumb *yourmodel.picturefield* 200 300 0 %}"/> <img src="{% thumb *yourmodel.picturefield* 500 400 yes %}"/> ... The parameters are: imagefield ImageField width Integer height Integer rescale [1, yes, true, 0, no, false] **Some codes come from <djangosnippets.org>**

  • django
  • image
  • templatetag
  • thumb
Read More

SQL Log Middleware - with multiple databases

This is an improvement of [joshua](http://djangosnippets.org/users/joshua/)'s [SQL Log Middleware](http://djangosnippets.org/snippets/61/). If you have more than one database connection, then all queries are logged, grouped by connection. If a connection has no queries, then it's not shown.

  • sql
  • middleware
  • log
  • multiple-databases
Read More

Improved Pickled Object Field (Fixed for Django 1.2)

Small changes to [Snippet 1694](http://djangosnippets.org/snippets/1694/) to that QueryAPI works for django 1.2 and higher. Changes: * Replaced `get_db_prep_value` with `get_prep_value`. * Replaced `get_db_prep_lookup` with modified `get_prep_lookup`.

  • model
  • db
  • orm
  • database
  • pickle
  • object
  • field
  • type
  • pickled
  • store
Read More

Group results by a range of dates in admin sidebar with calendar

Adds filtering by ranges of dates in the admin filter sidebar. [https://github.com/coolchevy/django-datefilterspec](https://github.com/coolchevy/django-datefilterspec) [http://coolchevy.org.ua](http://coolchevy.org.ua) https://github.com/coolchevy/django-datefilterspec/raw/master/datefilter.png Example: ` from django.db import models import datefilterspec class Person(models.Model): date = models.DateTimeField(default=datetime.datetime.now) date.date_filter = True class Admin: list_filter = ['date']

  • filter
  • admin
  • date
  • calendar
  • sidebar
  • filterspec
Read More

Google Analytics noscript tracking

Method which gets a chunk of HTML to perform standard Google Analytics tracking as well as a noscript version. Very useful for finding out what percentage of your users do not have JavaScript support. This is Python port of the PHP example here: [http://andrescholten.net/google-analytics-zonder-javascript/](http://andrescholten.net/google-analytics-zonder-javascript/) To use, you need to put the following into your settings file: GOOGLE_ANALYTICS_ID = 'UA-1234567-1' GOOGLE_ANALYTICS_DOMAIN = 'example.com'

  • google-analytics
  • noscript
Read More

2955 snippets posted so far.