Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 15, 2010 14:17:57

j.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Order a QuerySet by Method


Hallo,

I know, that the normal way to do that is to write the return value of
the method to the foreign model, always when a model that affects the
return value changes, and than order by that value of the model.

But I have a model whoose objects gets a specific state after they are
older than a specific number of days. And i don`t want to use a
cronjob or something like that to update the foreign model`s value.

So, can someone tell me, how I can order a query set by one of the
function of it`s models, in a pinch by reordering the obecjts in a new
object array or some kind like that.

There are not a lot of records I have to order, so I think there is no
performance problem for my kind of application.

Thank you!

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 15, 2010 14:22:03

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Order a QuerySet by Method


On Wednesday, December 15, 2010 1:34:15 PM UTC, josch wrote:
>
> Hallo,
>
> I know, that the normal way to do that is to write the return value of
> the method to the foreign model, always when a model that affects the
> return value changes, and than order by that value of the model.
>
> But I have a model whoose objects gets a specific state after they are
> older than a specific number of days. And i don`t want to use a
> cronjob or something like that to update the foreign model`s value.
>
> So, can someone tell me, how I can order a query set by one of the
> function of it`s models, in a pinch by reordering the obecjts in a new
> object array or some kind like that.
>
> There are not a lot of records I have to order, so I think there is no
> performance problem for my kind of application.
>
> Thank you!


You can convert the queryset to a list, then call the standard list sort
method on it, passing the method as the key:

results = MyModel.objects.filter(criteria=whatever)
results = list(results)
results.sort(key=lambda x: x.my_method())

Obviously, as you note, this is not as efficient as doing the sorting in the
database, but for small numbers of objects it should be acceptable.
--
DR.


--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 15, 2010 15:08:57

j.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Order a QuerySet by Method


Thank you!

On 15 Dez., 15:21, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Wednesday, December 15, 2010 1:34:15 PM UTC, josch wrote:
>
> > Hallo,
>
> > I know, that the normal way to do that is to write the return value of
> > the method to the foreign model, always when a model that affects the
> > return value changes, and than order by that value of the model.
>
> > But I have a model whoose objects gets a specific state after they are
> > older than a specific number of days. And i don`t want to use a
> > cronjob or something like that to update the foreign model`s value.
>
> > So, can someone tell me, how I can order a query set by one of the
> > function of it`s models, in a pinch by reordering the obecjts in a new
> > object array or some kind like that.
>
> > There are not a lot of records I have to order, so I think there is no
> > performance problem for my kind of application.
>
> > Thank you!
>
> You can convert the queryset to a list, then call the standard list sort
> method on it, passing the method as the key:
>
>     results = MyModel.objects.filter(criteria=whatever)
>     results = list(results)
>     results.sort(key=lambda x: x.my_method())
>
> Obviously, as you note, this is not as efficient as doing the sorting in the
> database, but for small numbers of objects it should be acceptable.
> --
> DR.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Dec. 18, 2010 17:11:25

j.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Order a QuerySet by Method


Another Question: How can I filter such an object list?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 17th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support