On Mon, 2009-01-26 at 14:20 -0800, Jeromie wrote: > I have a model with a status field that includes a choices argument to > provide human readable options. Because the options were changing > during development, the values stored in the database are rather > generic - S01, S02, etc. There is one view in my application that > provides a list of all the objects for this particular model with > several sort options, including the status field. I was originally > providing the ability to sort via a javascript plugin that examined > the DOM, but as the site has grown a single list for all the objects > has become unwieldy and had a significant negative impact on > performance. I'm going to use the Django pagination module to bring > things back in line, but this means I need to do my sorting on the > server side instead of the client side. > > There does not, unfortunately, appear to be any way to take advantage > of Django's sorting features to sort by the human readable name. I can > (and probably will) create a database table for my status values, > which would allow me to look up the human readable names and use it to > sort. I could also use list() to create a list to which I can add my > own sortable attributes, but I'm concerned that this may leave me with > performance problems.
Short answer: don't prematurely optimise! (Slightly) longer answer: iterating through a queryset brings all the objects into memory anyway. Pulling them into memory earlier and taking advantage of Python's awesome sorting ability to do some pre-processing isn't going to harm things. Now, that isn't completely equivalent when using the paginator, since the paginator will only read as many results as it needs for one page. However, unless you have tens of thousands of these suckers, the memory usage isn't really going to hurt you that much (and, if it does, you'll need to add some extra information at the database level to enable sorting to occur there .. see the next paragraph for why). > > Are there any simpler solutions that I may be overlooking? Ordering on querysets is done at the database level, via SQL. Since the human readable strings don't exist in the database, ordering cannot possibly use them. Thus, you have to do that kind of sorting at the Python level. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

