Login

Most bookmarked snippets

Snippet List

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

Class based generic views that automatically check permissions

Simple wrappers around the new class-based generic views (introduced in Django 1.3) that also check permissions. **Example Usage** *(views.py)*: from mymodule import RestrictedListView, RestrictedUpdateView class MyListView(RestrictedListView): model = MyModel class MyUpdateView(RestrictedUpdateView): model = MyModel and so on for Create and Delete.

  • generic-views
  • permissions
  • class-based
Read More

SelectDateWidget

This widget will produce a select box with the range of dates that you input. **Usage:** `widget=SelectDateWidget('2010-12-15', '2010-12-20')` **Output:** `<select> <option value="2010-12-15">Wed January 01, 2010</option> <option value="2010-12-16">Thu January 02, 2010</option> <option value="2010-12-17">Fri January 03, 2010</option> <option value="2010-12-18">Sat January 04, 2010</option> <option value="2010-12-19">Sun January 05, 2010</option> <option value="2010-12-20">Mon January 06, 2010</option> </select>`

  • datetime
  • select
  • widget
  • daterange
Read More

Compare two instances of the same model

This compares two objects (of the same model) and returns a tuple containing dictionaries with the changed attributes. Note that ALL attributes are run through comparison, so if you are modifying non-field attributes at runtime, these will also be included. Excluded keys is a tuple containing the names if attributes you do not want to include in the comparison loop (i.e. attributes which changes are irrelevant).

  • models
  • comparison
Read More

Prettify HTML body contents in HTTP response

This is an enhancement of snippet [#172](http://djangosnippets.org/snippets/172/). Here I use [BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/) — far more easier to install through pip in a virtualenv, and possibly a bit more maintained — to format and properly indent the rendered HTML from templates. I also added a check to only tidy contents in a `DEBUG=True` environment, regarding high impact on performance while doing so in production. Last, it's compatible with Django 1.2.x.

  • templates
  • format
  • html
  • tidy
  • syntax
  • output
  • prettify
  • formatting
  • indentation
  • readability
Read More

Limit ForeignKey filter values to those that have a relationship with current model

This is an updated snippet based on http://djangosnippets.org/snippets/2260/ The updated snippet can limit the filtering options for a foreign key field to only those entries that are related to the current model. I.e. if you have an Author model with a FK to Institution model, you can configure Author's changelist to include a filter on Institution, but only allow you to select institutions that have authors. Institutions that do not have authors won't show up on the list. To enable this, in your model's ModelAdmin class, set <fieldname>_fk_filter_related_only=True <fieldname>_fk_filter_name_field=<display this field of the related model in filter list> For example, in your AuthorAdmin class, you can do institution_fk_filter_related_only=True institution_fk_filter_name_field='name' Note that for the effect described above to work, you just need the last few lines of the big else clause in __init__, so if you don't care about filtering by FK property, you can just grab those few lines and create a simpler FilterSpec.

  • ForeignKey
  • Django-Admin
  • FilterSpec
Read More

Automatic urls for static pages

Create in your template dir html files named example.static.html and with this snippet you can get the static page with the url /example/. If you put static file in a sub-directory, the url will be /sub-directory/example/ **Example:** `static_urls = StaticUrls()` `urlpatterns = patterns('', *static_urls.discover())` `urlpatterns += patterns('',` `(r'^admin/doc/', include('django.contrib.admindocs.urls')),` `(r'^admin/', include(admin.site.urls)),` `)`

  • urls
  • static
  • static files
  • url pattern
Read More

ModelMixin

Enables convenient adding of fields, methods and properties to Django models. Instead of: User.add_to_class('foo', models.CharField(...) User.add_to_class('bar', models.IntegerField(...) you can write: class UserMixin(ModelMixin): model = User foo = models.CharField(...) bar = models.IntegerField(...)

  • mixin
  • metaclass
  • util
  • metaprogramming
Read More

Admin Word Count

I'm on my own personal writing challenge (see 750words.com for inspiration). I needed a way to get a dynamic word count, so I threw this together. Tied into django-basic-blog easily.

  • jquery
  • change-form
  • word-count
Read More

Custom FileField with content type and size validation

Usage described in this blog post: [Django: FileField with ContentType and File Size Validation](http://nemesisdesign.net/blog/coding/django-filefield-content-type-size-validation/) Snippet inspired by: [Validate by file content type and size](http://djangosnippets.org/snippets/1303/)

  • forms
  • validation
  • upload
  • filefield
  • file
  • content-type
  • max_upload_size
Read More

3110 snippets posted so far.