Login

Snippets by robharvey

Snippet List

filtered ModelChoiceField queries

ModelChoiceField allows you to use filtered queries to simplify your forms. This is great for adding objects but can fall down when you edit an existing object and the original query no longer contains the referenced field (e.g. I like to use an "active" field in several objects). The fix is simply to include an extra param: Q(pk=object_id). You have to do this in the __init__ method to get the object_id. A nice thing about this is that it works for ModelForms as well as custom Forms.

  • newforms
  • modelchoicefield
  • modelform
Read More

custom sql without table names

Keeps database table names out of custom SQL code, but still allows for correct parameter passing in the execute function. (psycopg doesn't substitute table or field names, only data, in the execute function)

  • sql
  • postgres
Read More

custom css classes for newforms

This isn't really any trick, its just that I didn't find any documentation of this or any references in searching. There are a few changes proposed for css classes which might make this redundant, but for now this works! If you want to add attributes for any fields, just include them in the widget constructor. They get written out in key value pairs when the input field is being created. e.g. in the example above, this will come out like: ` '<input type="text" name="start_date" id="id_start_date" class="vDateField required" size="10"/>' `

  • newforms
  • css
Read More

More admin clock time increments

This is quite a hack (need to modify Django code), but relatively simple and stable. It displays various times in whichever increments and columns you specify, rather than just Midnight, Noon, Now & 6am. You can use it throughout your admin and newforms-admin code. This is a (slightly updated) copy of the ticket #1848 which wasn't included in trunk. http://code.djangoproject.com/ticket/1848

  • admin
  • time
Read More

Simple admin list thumbnail view

This is a very simple way to display images within the admin list view. It is not efficient in the sense that the images are being downloaded in original format, however for cases where the images are not regularly accessed it may be a straightforward option. Can also be tied into WYSIWYG editors like TinyMCE by adding an appropriate href link in the return value.

  • image
  • admin
Read More

robharvey has posted 5 snippets.