Login

Most bookmarked snippets

Snippet List

CompressedTextField for Django 1.0+

This snippet updates http://www.djangosnippets.org/snippets/383/ for Django 1.0+, and adds support for sqlite3. Original snippet text: A CompressedTextField to transparently save data gzipped in the database and uncompress at retrieval.

  • text
  • model
  • field
  • compressed
  • gzip
Read More

Pretty print SQL of query sets

Install [sqlparse](http://code.google.com/p/python-sqlparse/) with `easy_install sqlparse` and then you can easily debug the SQL like this: def view(request): data = MyModel.objects.filter(something__very=complex) print_sql(data) ... Inspired by [Simon Willison](http://simonwillison.net/2009/Apr/28/)

  • sql
  • queryset
  • sqlparse
Read More

CrashKit Middleware

Catch exceptions and send it along with useful data to [CrashKit](http://crashkitapp.appspot.com/).

  • middleware
  • exception
  • crashkit
Read More
Author: wiz
  • 1
  • 2

Truncate HTML without breaking tags

Put it in appname/templatetags/truncatehtml.py and load it with {% load truncatehtml %}, then for instance {{ some_story|truncatehtml:100 }} to truncate the story to 100 characters. Tags are not counted in the length given, and character entities count as one character. The filter should never break an open-tag text close-tag sequence without adding in the close tag. It will also preserve character entities. It won't sanitize the HTML, though: garbage in, garbage out. There's a bit more info about how it works in a [blog post](http://ole-laursen.blogspot.com/2009/05/safe-truncation-of-html.html) I wrote.

  • template
  • filter
  • truncate
  • html
Read More

Simple Admin-button linking to Databrowse

If you want all the objects in your models' admin interface to have a direct link to their databrowse page (just like the *history* link).. follow these steps: 1) copy the *change_form.html* admin template in mytemplates/admin/ 2) edit *change_form.html* by adding this code right next to (before or after) the following line (=the history button markup): `<li><a href="history/" class="historylink">History</a></li>` 3) Make sure that all of you models are registered with [databrowse](http://docs.djangoproject.com/en/dev/ref/contrib/databrowse/) That's it. Obviously if you don't want ALL the models to have this link, you should use a different recipe, for example this one: [http://www.djangosnippets.org/snippets/1016/](http://docs.djangoproject.com/en/dev/ref/contrib/databrowse/)

  • django
  • admin
  • databrowse
  • button
Read More

convenience parent class for UserProfile model

This is just a reusable convenience parent class to allow you to create and administer an automatic user profile class using the following code: class UserProfile(UserProfileModel): likes_kittens = models.BooleanField(default=True) Whenever a `User` object is created, a corresponding `UserProfile` will also be created. That's it. NB: You will still need to set `AUTH_PROFILE_MODULE` in your settings :-) (PS: It would also be nice to have the resulting class proxy the `User` object's attributes like django's model inheritance does, while still automatically creating a `UserProfile` object when a `User` object is created :-)

  • userprofile
Read More

Query printer coroutine

If you would like to see the latest queries you have done when running a unittest, this is not so easy. You have to initialize the queries list and set DEBUG to True manually. Then you have to figure out a way to print the queries you want to see just now and format them. I.e., monitoring queries when doing TDD is a bit of a hassle, and this should help. This little helper does all this for you; your UnitTest only needs to import django.db.connection and store the current length (offset) of the queries list. Then, using Python's coroutine functionality, all you have to do is send that offset to the QueryPrinter coroutine (which you can, for example, initialize as a global variable or a class variable of your UnitTest) and you get the latest SQL printed as a simple table.

  • queries
  • unittest
  • debugging
  • printing
  • for
  • connection
Read More
Author: fnl
  • 0
  • 2

dumpdata/loaddata with MySQL and ForeignKeys, as django command

Based on [http://www.djangosnippets.org/snippets/662/](http://www.djangosnippets.org/snippets/662/) and updated to be runnable as custom django management command. Also added option support for --exclude=someapp --exclude=otherapp.SomeModel From original description: InnoDB tables within MySQL have no ability to defer reference checking until after a transaction is complete. This prevents most dumpdata/loaddata cycles unless the dump order falls so that referenced models are dumped before models that depend on them. Caveats 1. You use this snippet to dump the data and the built in manage.py loaddata to load the fixture output by this program. A similar solution could be applied to the XML processing on the loaddata side but this sufficed for my situations. 2. This code does not handle Circular or self-references. The loaddata for those needs to be much smarter

  • mysql
  • fixtures
  • dumpdata
  • command
Read More

Widget for Money values on Geraldo Reports

This is a widget for decimal/money/currency fields on **Geraldo Reports**. When you use Geraldo to write reports, decimal fields must be formatted using **get_value** lambda attribute, because ObjectValue doesn't know what mask you want to use. With this widget, you just copy it into a common use Python file, import into your reports file and use it replacing ObjectValue on elements for fields you must be formatted as money format. **Example:** from geraldo import Report, ReportBand, ObjectValue from utils.reports import DecimalObjectValue class ReportCustomers(Report): title = u'Customers List' page_size = A4 class band_detail(ReportBand): height = 0.5*cm elements = [ ObjectValue(attribute_name='id', top=0.1*cm), DecimalObjectValue(attribute_name='salary', left=26.2*cm, top=0.1*cm, format='%0.03f'), ]

  • geraldo
Read More

wordbreak filter

usage: {{ object.content|wordbreak:"10" }} This means if any word is 10 characters or longer, a `&shy;` will be placed every 10 characters. This is to break long words which may break the appearance of a page. The output of this is HTML safe as the content has been conditionally escaped.

  • filter
  • custom-filter
  • wordbreak
  • word-break
  • long-words
Read More

DiggBarMiddleware

give diggbar users something to think about (http://daringfireball.net/2009/04/how_to_block_the_diggbar)

  • middleware
Read More

Monitoring django-sphinx for Nagios

This snippet is used to monitor sphinx status via [django-sphinx](http://code.google.com/p/django-sphinx/). It returns 0 (OK) or 2 (CRITICAL). Remember to change this strings `ModelToMonitor` and `app_name`. Usage : `./manage your-controls-command --log`

  • sphinx
  • management
  • nagios
  • monitoring
  • django-sphinx
Read More

get next mysql autoincrement value

i use this to get the pk of a record before creation, in my scenario to name an uploaded image: def UPLOADTO( i,n ): if not i.id: id = get_nextautoincrement( i.__class__ ) else: id = i.id return str(id)+'.jpg'

  • mysql
  • autoincrement
Read More

3110 snippets posted so far.