0

In my application I do the following lookup:

my_datapoint = Datapoint.objects.filter(timestamp_lte = desired_time).reverse()[0]

I have to do this several times, for records that are not adjacent in time.

Is there a way to make this more efficient than having several separate QuerySets? Can I combine them?

2 Answers 2

4

This has been asked a ton of times on here. You can use chain

from itertools import chain

combined = chain(qs1, qs2, ...)
for item in combined:
    # foo

One alternative to completely separate queryset objects, is to see if you can do it with "OR" queries using the Q objects:
https://docs.djangoproject.com/en/1.4/topics/db/queries/#complex-lookups-with-q-objects

Example from the docs

Poll.objects.get(
    Q(question__startswith='Who'),
    Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6))
)

This example says "objects that have a question starting with 'Who', AND, objects with either this pub date OR that pub date"

Sign up to request clarification or add additional context in comments.

2 Comments

Would this combine actual calls to the database?
@Goro: No, You can't do it unless you actually form a single query object or write a raw query. You can't take completely separate query sets and combine them into a single db call unfortunately. It would probably have to do a lot of checking to be able to combine them logically.
1

By "more efficient", I assume you mean you want to avoid hit database multiple times. In that case, you should query once, and then loop through it yourself, also use QuerySet to sort is better than reverse()

my_datapoint = Datapoint.objects.filter(timestamp_lte = max_desired_time).order_by('-timestamp')
def getLatest(desired_time):
    for item in my_datapoint:
        if item.timestamp <= desired_time:
            return item

If you just want simpler syntax, use chain as jdi suggested.

1 Comment

Ya this woud be the alternate suggestion if the OP was indeed looking to do it in one call. Get more data from the server, and filter locally on the client.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.