Login

Most bookmarked snippets

Snippet List

Prefill ForeignKey caches

Provides an efficient means of looking up multiple related model instances for a range of objects by pre-filling the cache attribute used by `SingleRelatedObjectDescriptor` with either complete model instances or a dict containing only specified fields, looking up all required data with a single query. Example usage: C:\django_projects\soclone>django-admin.py shell >>> from soclone.models import Question >>> from soclone.views import populate_fk_caches >>> from django.db import connection >>> from django.contrib.auth.models import User >>> q = Question.objects.get(id=1) >>> a = list(q.answers.all()) >>> connection.queries = [] >>> populate_fk_caches(User, ( ... ((q,), ('author', 'last_edited_by', 'closed_by')), ... (a, ('author', 'last_edited_by')), ... ), ... fields=('username', 'gravatar', 'reputation', 'gold', 'silver', ... 'bronze')) >>> connection.queries [{'time': '0.000', 'sql': u'SELECT "auth_user"."id", "auth_user"."username", "au th_user"."gravatar", "auth_user"."reputation", "auth_user"."gold", "auth_user"." silver", "auth_user"."bronze" FROM "auth_user" WHERE "auth_user"."id" IN (1, 2)' }]

  • foreignkey
  • cache
  • prefill
Read More

Mod to allow simple_tag to access context

This is a mod I made to the Django simple_tag system to let the simple_tags access comments. I plan to try and get it integrated into the trunk, so it's mainly here so (a) the people on django-developers can see it, and (b) while I'm waiting, or if it doesn't get put in the trunk, people can use it. **Installing** 1. Open the module `django.template.__init__`, wherever that lives. 2. Scroll down to the beginning of " `class Library:` " 3. Find the simple_tag function (" `def simple_tag(self,func):` ") 4. Replace the function (even the whitespace before each line) with the code snippet. **Usage** 1. When defining a simple tag (see the [docs](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#shortcut-for-simple-tags) for more info), have the first parameter in your function be named "`context`". Otherwise, an error will be thrown. It can accept other parameters like normal. 2. Use `register.simple_tag(my_function_name, takes_context=True)` to register your function as one that will use the context. 3. The tag's function can access the context like a dictionary - to get a value, just use `context['cheese']`, or to set one, use `context['cheese'] = 'Limberger'`. Due to the context's mutability, this will affect the context in the main template as well. **Notes** This code hasn't been tested in a "production environment", but I did test my modifications thoroughly, and if you don't add `takes_context=True`, `simple_tag` will behave exactly as normal. Of course, if there is a problem, make sure you leave a comment. **Code** Since most of the code is hacked up from other Django library functions, and to prepare for if and when it's merged into the trunk, it's released under the BSD license.

  • template
  • template-tags
  • template-engine
Read More

Simple Signal to denormalize vote counts in django-voting

This attaches a signal to the save and delete signals for the Vote object and recalculates the score for the object and stores it in that object's vote_score attribute. This allows you to have a list of those objects and not have to calculate in the database the vote score for each object.

  • voting
  • denormalization
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

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

easy admin registration

This essentially wraps [snippet 917](http://www.djangosnippets.org/snippets/917/) (with full credit to author ncw) in a convenience function so that you can type: admin_register(admin, namespace=globals()) or more concisely: admin_register(admin, globals()) at the end of your admin.py file without having to register each model and admin class individually.

  • admin
  • register
Read More

SQL Function Decorator

This decorator will replace a method on a model with a class method that executes SQL in the functions doc string.

  • sql
  • model
  • decorator
Read More

Translation statistics gatherer

A script that gathers statistics of translated, untranslated and fuzzy literals of translations (be it Django itself or a project using Django). For that it re-scans the tree and generates a up-to-date POT in a temporary location, so the statistics of translation "coverage" are calculated relative to the current status of the tree. It doesn't touch the tree it is analyzing at all. It should be run from the directory containing the `locale/` directory of your project or from the `django/` directory of a Django copy. It is based on the `makemessages` Django management command (or rather its previous standalone `make-messages.py` script incarnation) and uses the same command line switches: * `-d <domain>` -- `<domain>` is `django` or `djangojs`. Optional, defaults to `django`. * `-l <language>` OR * `-a` -- process all languages

  • internationalization
  • i18n
  • l10n
  • translations
  • status
  • statistics
  • localization
Read More

MODPYTHON Sample Site Logging

This sample site_logging.py module uses the [MODPYTHON Logging snippet](http://www.djangosnippets.org/snippets/627/). It assigns the ApacheLogHandler class to the main logging object, when Django is run inside MODPYTHON. You must have the MODPYTHON Logging snippet, and name it modpython_logging.py for this to work. Apache will write to virtual host-specific logs only with Python 2.5. Users are encouraged to change the two logging settings. Log Level: handler.setLevel(logging.WARNING) Log Format: logging.Formatter(...)

  • logging
  • mdpython
Read More

Replace Paragraph Tags for Flash

This template tag does two things needed to display content from something like a TextField wrapped with TinyMCE in Flash's htmlText component: 1. Replace `<p>` tags with `<br />` 2. Strip all occurances of `\n`

  • template
  • tag
Read More

Pledgie data parser

This one uses - and is very similar to - http://www.djangosnippets.org/snippets/852/ It gets the data from a pledgie.com campaign and parses it.

  • pledgie
  • donation
  • parser
Read More

FilteredSelect

Displays as an ordinary selectbox with an additional text-input for filtering the options. An adaption of the example at [1] for use with Django. The code assumes the java script from [2] is downloaded into your media folder. Remember to output your form's media, for instance like: "{{form.media|safe}}", somewhere above your form. **License:** [2] by Mr Patrick Fitzgerald is licensed under GPL and the python code written by myself is GPL as well. **References:** 1. http://www.barelyfitz.com/projects/filterlist/index.php/ 2. http://www.barelyfitz.com/projects/filterlist/filterlist.js

  • javascript
  • widget
Read More

3110 snippets posted so far.