Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django QuerySet order

Tags:

I am new to both django and python but I am beginning to get a grasp on things. I think.

I have this problem and I can not seem to find an answer to it. (Though I assume it is really simple and the limiting factors are my google skills and lack of python/django knowledge)

The scenario:

a user can opt in to recieve temporary job openings at any number of stores he or she chooses.

I would like to present a list of upcoming job openings (StoreEvents) sorted by the DateField only.

Example: 
    Store A - 2009-04-20 
    Store B - 2009-04-22
    Store A - 2009-04-23

Atm I am stuck with presenting the data first sorted by store then by date since I am obviously accessing the StoreEvents through the Store model.

Example:
    Store A - 2009-04-20
    Store A - 2009-04-23
    Store B - 2009-04-22

So my question is: Is it possible to create a QuerySet that looks like the first example and how do I do it?

Examples of related models included:

class Store(models.Model):

class StoreEvent(models.Model):
    info = models.TextField()
    date = models.DateField()
    store = models.ForeignKey(Store, related_name='events')

class UserStore(models.Model):
    user = models.ForeignKey(User, related_name='stores')
    store = models.ForeignKey(Store, related_name='available_staff')

Edit:

The following SQL does the trick but I still can't figure out how to do it in django:

SELECT *
FROM store_storeevent
WHERE store_id
IN (
    SELECT store_id
    FROM profile_userstore
    WHERE user_id =1
)
ORDER BY date
like image 992
Martin Avatar asked Apr 17 '09 17:04

Martin


People also ask

Does order of filter matter Django?

If you are just doing two simple filter operations, then you're correct that order doesn't matter, but be careful. There are examples of when the order of your queryset methods do matter: https://docs.djangoproject.com/en/dev/topics/db/aggregation/#order-of-annotate-and-filter-clauses.

How do I sort by date in Django?

This answer Django Datetime Field Query - Order by Time/Hour suggests that I use '__day' with my date_modified as in: Article. objects. filter().

What is Queryset in Django?

A QuerySet is a collection of data from a database. A QuerySet is built up as a list of objects. QuerySets makes it easier to get the data you actually need, by allowing you to filter and order the data.


2 Answers

Order by Date for all users:

queryset = StoreEvent.objects.all().order_by('-date')

To filter by user:

queryset = StoreEvent.objects.filter(stores__user=request.user).order_by('-date')
like image 110
Harold Avatar answered Oct 15 '22 12:10

Harold


Thanks for your help guys, finally figured it out:

qs = StoreEvent.objects.filter(
    store__in=Store.objects.filter(
        available_staff__in=UserStore.objects.filter(user=user)
    )
).order_by('date')

it results in 3 SQL SELECTs but does the trick...

like image 30
Martin Avatar answered Oct 15 '22 12:10

Martin