Django custom fields: descriptor class gotcha

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

quickie: jQuery array comparison helper

I was looking for an answer to this question today an ended up answering it myself.

jQuery.extend({
    arrayCompare: function (arrayA, arrayB) {
        if (arrayA.length != arrayB.length) { return false; }
        // sort modifies original array
        // (which are passed by reference to our method!)
        // so clone the arrays before sorting
        var a = jQuery.extend(true, [], arrayA);
        var b = jQuery.extend(true, [], arrayB);
        a.sort(); 
        b.sort();
        for (var i = 0, l = a.length; i < l; i++) {
            if (a[i] !== b[i]) { 
                return false;
            }
        }
        return true;
    }
});

var a = [1, 2, 3];
var b = [2, 3, 4];
var c = [3, 4, 2];

jQuery.arrayCompare(a, b);
// false

jQuery.arrayCompare(b, c);
// true

// c is still unsorted [3, 4, 2]

Python-style template strings for AS3

http://gist.github.com/613564

Currently only handles the ‘dict of vars’ style substitution and probably doesn’t handle escaping and other edge-cases properly. Fine for a bit of quick and dirty string action though.

Doing my bit to Promote JS

JavaScript JS Documentation: JS Array slice, JavaScript Array slice, JS Array .slice, JavaScript Array .slice
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.

JS+Django: streaming multi-part ajax responses (MXHR)

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.”

Read more of this post

Django: export CSV admin action

Just a quickie…

If you’ve ever needed this:

Then here’s some handy code to do that.

You can customise and use this action (actually a sort of action factory function) with any of your models which need it.

Django: optiongroups for your ModelChoice Field

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!

Follow

Get every new post delivered to your Inbox.