Login

Snippets by anentropic

Snippet List

Ultimate(?) export/download CSV admin action

This owes a debt to a number of earlier snippets by myself and others, including: *(most directly)* [#2868](http://djangosnippets.org/snippets/2868/), plus [#2020](http://djangosnippets.org/snippets/2020/), [#2712](http://djangosnippets.org/snippets/2712/), [#1697](http://djangosnippets.org/snippets/1697/) Use of OrderedDict means it requires Python 2.7+. You also need to `pip install singledispatch` which is a backport of a Python 3.4 feature. Singledispatch (along with custom attributes instead of a factory function) gives a very clean interface in your ModelAdmin, whether or not you need a custom `short_description`. This version allows you to (optionally) specify custom column labels, or to (optionally) use Django's usual prettifying mechanism (`field.verbose_name`). Thanks to #2868 it can do relation-spanning double-underscore lookups and also model attribute/method (rather than field) lookups for columns, just like you can in your ModelAdmin `list_display`.

  • csv
  • admin-actions
Read More

Generic compact list_filter with counts

Based on [#1879](http://djangosnippets.org/snippets/1879/) and [#2356](http://djangosnippets.org/snippets/2356/) Works in Django 1.3 Hopefully it's generic enough to implement a compact (sparse) version of whatever custom filter you need.

  • admin-interface
  • compact
  • list_filter
Read More

ModelChoiceField with option groups

This is a ModelChoiceField where the choices are rendered in optiongroups (this is already posible with a normal Choicefield) For this to work properly the queryset you supply should already be ordered the way you want (i.e. by the group_by_field first, then any sub-ordering)

  • modelchoicefield
Read More

create or update, then get, model instances from JSON/py dict

Basically the idea is to import/update model instances from a json data that closely matches the model structure (i.e. identical field names) From my answer to this question: [http://stackoverflow.com/a/8377382/202168](http://stackoverflow.com/a/8377382/202168) See the original question for sample data format.

  • json
  • import
Read More

Generic CSV export admin action factory

**I've since made a better snippet for this: [#2995](http://djangosnippets.org/snippets/2995/)** based on [#1697](http://djangosnippets.org/snippets/1697/) This one is even more generic since you can specify which fields to include or exclude, a custom description text for the drop-down menu and whether to output the header row.

  • csv
Read More

ModelChoiceField with optiongroups

This is a ModelChoiceField where the choices are rendered in optiongroups (this is already posible with a normal Choicefield) For this to work properly the queryset you supply should already be ordered the way you want (i.e. by the group_by_field first, then any sub-ordering) See [related blog article](http://anentropic.wordpress.com/2010/03/23/django-optiongroups-for-your-modelchoice-field/)

  • modelchoicefield
  • optiongroup
Read More

Exclusive boolean field

**NOTE: I now have a better implementation of this (nicer api, less signal wrangling) available [on PyPI here](https://pypi.python.org/pypi/django-exclusivebooleanfield)** Sometimes you want to be able to make one (and only one) row in your model 'featured' or 'the default one' If you have some kind of parent model you could have a ForeignKey on the parent to hold that info, but that won't always be the case - eg you may have no parent, or multiple parent models. With the exclusive_boolean_fields() helper you can do it with or without a parent model and it possibly makes the admin interface a bit simpler.

  • field
Read More

Ajax auto-filtered Foreign Key Field

Say you have a ModelChoiceField and you want the choices to depend on the value of another field in the form... that's what these bits are for. They need to be used in conjunction with the Ajax views from: [code.google.com/p/django-ajax-filtered-fields/](http://code.google.com/p/django-ajax-filtered-fields/) See my blog for full details: [anentropic.wordpress.com](http://anentropic.wordpress.com) ...um, this is work in progress and the explanatory blog post is not up yet...

  • ajax
  • widget
Read More

django-mptt enabled replacement for SelectBox.js

This snippet is used in conjunction with the code in [#1779](http://www.djangosnippets.org/snippets/1779/) to make an mptt-enabled version of the FilteredSelectMultiple widget. See my blog for full details: [http://anentropic.wordpress.com](http://anentropic.wordpress.com/2009/11/05/more-django-mptt-goodness-filteredselectmultiple-m2m-widget/)

  • widget
  • mptt
Read More

django-mptt enabled FilteredSelectMultiple m2m widget

If you are using django-mptt to manage content (eg heirarchical categories) then it needs a bit of help to make a nice admin interface. For many-to-many fields, Django provides the quite nice FilteredSelectMultiple widget (a two-pane selection list with search box) but it only renders 'flat' lists... if you have a big category tree it's going to be confusing to know what belongs to what. Also, list items are sorted alphabetically in the js, which won't be what you want. This snippet extends FilteredSelectMultiple to show the tree structure in the list. You'll also need the js from this snippet: [#1780](http://www.djangosnippets.org/snippets/1780/) For usage details see my blog at: [http://anentropic.wordpress.com](http://anentropic.wordpress.com/2009/11/05/more-django-mptt-goodness-filteredselectmultiple-m2m-widget/)

  • widget
  • mptt
Read More

MPTTModelAdmin

This builds on a couple of other people's hacks to effectively manage django-mptt models via the admin. One problem you find is if you use the actions drop-down menu to ‘delete selected’ items from your mptt model… the bulk actions bypass the model’s delete method so your left/right values for the tree aren’t updated. What you need is a way to automatically rebuild the tree after a bulk action. This code provides that facility. Full details on my blog: [http://anentropic.wordpress.com/2009/10/26/](http://anentropic.wordpress.com/2009/10/26/making-admin-bulk-delete-action-work-with-django-mptt/)

  • admin
  • mptt
Read More

anentropic has posted 11 snippets.