Let’s say you are making a custom field type that uses different attname and name properties, like ForeignKey fields do.

Probably this is so that you can store the raw value in the db under field.attname, while transparently accessing a converted value via field.name

Probably to make that mechanism work you are needing a descriptor class, much like a ForeignKey field has a ReverseSingleRelatedObjectDescriptor which handles the get/set functionality for the field – allowing you to get or set model instances, while a plain id is saved in the db under field.attname.

Since there’s no specific docs on how to do this you probably started by copying Django’s example, like the ForeignKey mechanism above. Read more of this post

This is a great campaign to bump up the Google rank of proper Javascript docs, to get them up above the noise, making them easier to find. I’m doing my bit by posting this here, if you want to help then get your own code and join in.

Ajax is great eh? What did it stand for again…? Asynchronuous Javascript And XML?

Anyone actually sending back XML in their Ajax app these days?

I find, if I have the choice, I’m using JSON for sending the data back. Then, for those times where you don’t want to duplicate all of the templating logic from the server in your Javascript code just to ‘Ajax’ a form or something, I find I will often render a bit of HTML and send that back, even if it feels a bit dirty. It’s easy and there’s a certain DRYness that’s undeniable.

So it’s only a matter of time before you start thinking, as a did, “Sure, I could make half a dozen separate XHR requests, but what I really need to do here is send back a package of identifiable HTML and JSON chunks.”

With a normal Django ChoiceField you can specify choices either as a simple tuple of tuples (value, label pairs) or with an extra level structure to organise the choices into named groups. The Django Select widget will then happily render the choices into html optiongroups.

With a ModelChoiceField you don’t specify the choices directly, you give a queryset of results from the db, which just has a flat structure.

I’ve made a ‘GroupedModelChoiceField’ that allows to output optiongroups based on a field from the model in your queryset. Code is here on djangosnippets… simple as that!