#26141: __lt__ for model instances
----------------------------------------------+--------------------
Reporter: srkunze | Owner: nobody
Type: New feature | Status: new
Component: Database layer (models, ORM) | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
We need to sort a list of model instances (not a queryset).
After some research, we didn't find a standard way to do this with Django
model instances. Would it be possible to implement {{{__lt__}}} for model
instances via the meta class mechanism?
Currently, we write a wrapper where we implemented the following
{{{__lt__}}} method:
{{{
def __lt__(self, other):
for criteria in self.object._meta.ordering:
field = criteria[1:] if criteria.startswith('-') else criteria
mine = getattr(self.object, field)
theirs = getattr(other.object, field)
if isinstance(mine, unicode):
mine = locale.strxfrm(mine.encode('utf8'))
theirs = locale.strxfrm(theirs.encode('utf8'))
if mine == theirs:
continue
if criteria.startswith('-'):
return mine > theirs
return mine < theirs
raise NotImplementedError
}}}
Please note, that the implementation is locale-aware.
--
Ticket URL: <https://code.djangoproject.com/ticket/26141>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/050.b196112819b8bd0aa479a36fba450301%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.