djangosnippets.org: Latest snippets posted by anentropichttps://djangosnippets.org/users/anentropic/2013-10-02T07:23:13.270131-05:00anentropicUltimate(?) export/download CSV admin action
2013-10-02T07:23:13.270131-05:00anentropichttps://djangosnippets.org/snippets/2995/<p>This owes a debt to a number of earlier snippets by myself and others, including:
<em>(most directly)</em> <a href="http://djangosnippets.org/snippets/2868/">#2868</a>, plus <a href="http://djangosnippets.org/snippets/2020/">#2020</a>, <a href="http://djangosnippets.org/snippets/2712/">#2712</a>, <a href="http://djangosnippets.org/snippets/1697/">#1697</a></p>
<p>Use of OrderedDict means it requires Python 2.7+. You also need to <code>pip install singledispatch</code> which is a backport of a Python 3.4 feature.</p>
<p>Singledispatch …</p>
Freely redistributableGeneric compact list_filter with counts
2012-01-12T14:06:03.612043-06:00anentropichttps://djangosnippets.org/snippets/2644/<p>Based on <a href="http://djangosnippets.org/snippets/1879/">#1879</a> and <a href="http://djangosnippets.org/snippets/2356/">#2356</a></p>
<p>Works in Django 1.3</p>
<p>Hopefully it's generic enough to implement a compact (sparse) version of whatever custom filter you need.</p>
Freely redistributableModelChoiceField with option groups
2011-12-05T08:57:33.192442-06:00anentropichttps://djangosnippets.org/snippets/2622/<p>This is a ModelChoiceField where the choices are rendered in optiongroups
(this is already posible with a normal Choicefield)</p>
<p>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)</p>
Freely redistributablecreate or update, then get, model instances from JSON/py dict
2011-12-05T07:50:10.129560-06:00anentropichttps://djangosnippets.org/snippets/2621/<p>Basically the idea is to import/update model instances from a json data that closely matches the model structure (i.e. identical field names)</p>
<p>From my answer to this question: <a href="http://stackoverflow.com/a/8377382/202168">http://stackoverflow.com/a/8377382/202168</a></p>
<p>See the original question for sample data format.</p>
Freely redistributableGeneric CSV export admin action factory
2010-05-14T09:19:45.161732-05:00anentropichttps://djangosnippets.org/snippets/2020/<p><strong>I've since made a better snippet for this: <a href="http://djangosnippets.org/snippets/2995/">#2995</a></strong></p>
<p>based on <a href="http://djangosnippets.org/snippets/1697/">#1697</a></p>
<p>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.</p>
Freely redistributableModelChoiceField with optiongroups
2010-03-23T11:36:35.330608-05:00anentropichttps://djangosnippets.org/snippets/1968/<p>This is a ModelChoiceField where the choices are rendered in optiongroups
(this is already posible with a normal Choicefield)</p>
<p>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)</p>
<p>See <a href="http://anentropic.wordpress.com/2010/03/23/django-optiongroups-for-your-modelchoice-field/">related blog article</a></p>
Freely redistributableExclusive boolean field
2009-12-08T06:58:29.727524-06:00anentropichttps://djangosnippets.org/snippets/1830/<p><strong>NOTE: I now have a better implementation of this (nicer api, less signal wrangling) available <a href="https://pypi.python.org/pypi/django-exclusivebooleanfield">on PyPI here</a></strong></p>
<p>Sometimes you want to be able to make one (and only one) row in your model 'featured' or 'the default one'</p>
<p>If you have some kind of parent model you could have …</p>
Freely redistributableAjax auto-filtered Foreign Key Field
2009-11-10T07:32:34.460862-06:00anentropichttps://djangosnippets.org/snippets/1788/<p>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.</p>
<p>They need to be used in conjunction with the Ajax views from:
<a href="http://code.google.com/p/django-ajax-filtered-fields/">code.google.com/p/django-ajax-filtered-fields/</a></p>
<p>See my blog for full details:
<a href="http://anentropic.wordpress.com">anentropic.wordpress.com</a></p>
<p>...um, this is …</p>
Freely redistributabledjango-mptt enabled replacement for SelectBox.js
2009-11-05T03:53:10.604505-06:00anentropichttps://djangosnippets.org/snippets/1780/<p>This snippet is used in conjunction with the code in <a href="http://www.djangosnippets.org/snippets/1779/">#1779</a> to make an mptt-enabled version of the FilteredSelectMultiple widget.</p>
<p>See my blog for full details:
<a href="http://anentropic.wordpress.com/2009/11/05/more-django-mptt-goodness-filteredselectmultiple-m2m-widget/">http://anentropic.wordpress.com</a></p>
Freely redistributabledjango-mptt enabled FilteredSelectMultiple m2m widget
2009-11-05T03:38:22.804205-06:00anentropichttps://djangosnippets.org/snippets/1779/<p>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 …</p>
Freely redistributableMPTTModelAdmin
2009-10-26T12:06:42.824017-05:00anentropichttps://djangosnippets.org/snippets/1775/<p>This builds on a couple of other people's hacks to effectively manage django-mptt models via the admin.</p>
<p>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 …</p>
Freely redistributable