djangosnippets.org: Latest snippets posted by jpichttp://djangosnippets.org/users/jpic/2017-09-17T16:29:15.499988-05:00jpicAutomigrate, autocreatesuperuser if not User.count() in runserver and use manage.py:main as entrypoint
2017-09-17T16:29:15.499988-05:00jpichttp://djangosnippets.org/snippets/10622/<p>With this awesome manage.py, it will try to migrate first when called with runserver.</p>
<p>Also, this manege.py has super power to be used in your entry point as such:</p>
<pre><code>entry_points = {
'console_scripts': [
# u haz a setup.py -> u haz importable module :)
'yourcommand = yourproject.manage:main',
],
}, …</code></pre>
Freely redistributablehaystack auto update (forget about update_index)
2012-01-17T12:22:01.481895-06:00jpichttp://djangosnippets.org/snippets/2649/<p>Dead simple snippet. Paste it in some models (i use project_specific/models.py), and you don't need to run update_index anymore. When a model is deleted from the database: it is deleted from the index. When a model is saved (created or modified): it is updated in the index.</p>
Freely redistributablePrefetch generic relations
2011-07-19T05:20:59.399568-05:00jpichttp://djangosnippets.org/snippets/2492/<p>See the function docstring.</p>
Freely redistributableTest if sessions are working
2010-03-01T02:21:18.391137-06:00jpichttp://djangosnippets.org/snippets/1946/<p>I wasn't sure if my setup supported sessions properly. This view helped me make sure.
</p>
<p>Usage: fill the inputs with text and make sure that these values traverse a couple of requests. If it doesn't work then maybe the session backend you've set is broken?
</p>
Freely redistributableJavascript constraints in admin app and fieldsets to tabs (jquery)
2009-03-09T12:33:39.503233-05:00jpichttp://djangosnippets.org/snippets/1358/<p>A proper app was published for this: https://github.com/yourlabs/django-dynamic-fields</p>
<p>Try it out !</p>
Freely redistributableLazy options on ModelForm fields - like setting a ModelChoiceField queryset from the view
2008-12-31T06:38:43.871329-06:00jpichttp://djangosnippets.org/snippets/1272/<p>Example view code:</p>
<pre><code>lazy_field_options = {
'field_name_that_is_m2m': {
'queryset': YourRelatedModel.objects.filter(groups=request.user.groups.all()),
},
'field_name_that_is_fk': {
'queryset': YourOtherRelatedModel.objects.filter(slug=request_slug),
},
}
modelform = YourModelForm(jpic_field_options=lazy_field_options)
# after the modelform has called for parent __init__, it will set
# options for each field if possible.
</code></pre>
Freely redistributableAllow editing of the selected object of the filter_vertical/filter_horizontal widget with jquery
2008-10-10T11:21:04.017135-05:00jpichttp://djangosnippets.org/snippets/1125/<p>This patch allows to open a popup to edit the selected object of either the left or right select input of a filter_horizontal or filter_vertical field, when pressing the "Insert" key.</p>
<p>Being a noob with general client-side issue, it's served as a patch that suits my needs. Maybe it's possible …</p>
Freely redistributableMulti-level (tree-ish) navigation
2008-09-28T13:28:59.424346-05:00jpichttp://djangosnippets.org/snippets/1091/<p>An old snippet I made in my first django project. Nowadays I code menus in HTML and just use the perms proxy: https://docs.djangoproject.com/en/1.0/topics/auth/#id6</p>
<p>Credits, (awsome persons that helped me getting it to work efficiently and for free):</p>
<ul>
<li>Yhg1s and waveform from #python@freenode</li>
<li>zendak from #django@freenode</li>
</ul>
<p>Thank you!</p>
Freely redistributable