(tables) change column (not row) order in ModelTable

170 views
Skip to first unread message

MrJogo

unread,
Feb 12, 2009, 1:42:41 AM2/12/09
to Django Apps
Is there a way to change the order of columns in a ModelTable? I
created a ModelTable and added an extra field that should go between
two of the model fields, but instead always gets rendered at the end.
I tried explicitly declaring the fields in the order I want them, but
that does not work. Might be a nice feature.

Michael Elsdörfer

unread,
Feb 12, 2009, 11:16:20 PM2/12/09
to Django Apps
It works pretty much like Django models and forms do, i.e. a "creation
counter" ensures that columns are in the order in which they have been
defined (by default, Python stores class attributes as a dict which of
course does not support ordering).

Functionality to change the order *after* the table class or an
instance has been created would certainly be possible, but I'm not
sure yet how I feel about that.

You do have a valid problem - the same problem one has right now with
Form inheritance/ModelForms (and to a limited extend also with Models,
though order there obviously doesn't matter all that match). I would
like to stay true to the approach Django settles on - unfortunately,
they don't seem to have yet. Related tickets include #5986 and #8164
(*)

In the meantime, the concept from this forms recipe can also be
applied to django-tables and might help you along:

http://code.djangoproject.com/wiki/CookBookNewFormsFieldOrdering

The columns are stored in a SortedDict, in "TableClass.base_columns"
or "table_instance.columns._columns".

(*) *http://code.djangoproject.com/ticket/5986, http://code.djangoproject.com/ticket/8164

MrJogo

unread,
Feb 20, 2009, 4:02:54 PM2/20/09
to Django Apps
Great! Thanks for the response.

On Feb 12, 8:16 pm, Michael Elsdörfer <elsdoer...@gmail.com> wrote:
> It works pretty much like Django models and forms do, i.e. a "creation
> counter" ensures that columns are in the order in which they have been
> defined (by default, Python stores class attributes as a dict which of
> course does not support ordering).
>
> Functionality to change the order *after* the table class or an
> instance has been created would certainly be possible, but I'm not
> sure yet how I feel about that.
>
> You do have a valid problem - the same problem one has right now with
> Form inheritance/ModelForms (and to a limited extend also with Models,
> though order there obviously doesn't matter all that match). I would
> like to stay true to the approach Django settles on - unfortunately,
> they don't seem to have yet. Related tickets include #5986 and #8164
> (*)
>
> In the meantime, the concept from this forms recipe can also be
> applied to django-tables and might help you along:
>
> http://code.djangoproject.com/wiki/CookBookNewFormsFieldOrdering
>
> The columns are stored in a SortedDict, in "TableClass.base_columns"
> or "table_instance.columns._columns".
>
> (*) *http://code.djangoproject.com/ticket/5986,http://code.djangoproject.com/ticket/8164

MrJogo

unread,
Feb 20, 2009, 4:02:54 PM2/20/09
to Django Apps
Great! Thanks for the response.

On Feb 12, 8:16 pm, Michael Elsdörfer <elsdoer...@gmail.com> wrote:
> It works pretty much like Django models and forms do, i.e. a "creation
> counter" ensures that columns are in the order in which they have been
> defined (by default, Python stores class attributes as a dict which of
> course does not support ordering).
>
> Functionality to change the order *after* the table class or an
> instance has been created would certainly be possible, but I'm not
> sure yet how I feel about that.
>
> You do have a valid problem - the same problem one has right now with
> Form inheritance/ModelForms (and to a limited extend also with Models,
> though order there obviously doesn't matter all that match). I would
> like to stay true to the approach Django settles on - unfortunately,
> they don't seem to have yet. Related tickets include #5986 and #8164
> (*)
>
> In the meantime, the concept from this forms recipe can also be
> applied to django-tables and might help you along:
>
> http://code.djangoproject.com/wiki/CookBookNewFormsFieldOrdering
>
> The columns are stored in a SortedDict, in "TableClass.base_columns"
> or "table_instance.columns._columns".
>
> (*) *http://code.djangoproject.com/ticket/5986,http://code.djangoproject.com/ticket/8164
Reply all
Reply to author
Forward
0 new messages