On Fri, 2007-03-23 at 21:09 +0000, Andrew M. wrote:
> I lied, I don't know what I'm doing...
> 
> Malcolm/Anyone with the time to provide some, or all :-), example code
> for the above, I would really appreciate it...

The simplest solution would be something like this:

You must have a way to compare your two types of objects against each
other (so that you can put them in order, otherwise you might as well
just concatenate them). So let's suppose that you have a function
sorter() which takes two objects, compares them and returns the usual
-1/0/+1 results for a comparison function (-1 if the first arg is
"less", +1 if it's "more" and 0 if the two args are equal). This
function should be able to compare two objects of the same type, or one
object from each of your QuerySets, so you may need to check the type of
objects before doing the comparison.

Then you can sort the combined list of objects as follows:

        result = list(queryset1) + list(queryset2)
        result.sort(sorter)
        
If you've only got a few dozen (or even a few hundred) results in your
querysets, there are no real efficiency problems here. This code does
suck all the results into memory on the Python side, but for small
result sets, that's fine.
        
The sorter() function might be as simple as this (assuming both types of
objects have a "date" attribute):

        def sorter(lhs, rhs):
                return cmp(lhs.date, rhs.date)
        
It would need to be more complex if you need to compare, say, the "date"
attribute for one type of object with the "created" attribute on the
second type, but I hope you can see the pattern.

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
-~----------~----~----~----~------~----~------~--~---

Reply via email to