[Django]-Django QuerySet order

9👍

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…

👤Martin

47👍

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')
👤Harold

1👍

queryset = StoreEvent.objects.filter(store__in=UserStore.objects.filter(user__id=1).store).order_by('store__name', '-date')

or more graciously

user = User.objects.get(username="foo")
user_stores = user.stores.all()

store_events = StoreEvent.objects.filter(store__in=user_stores).order_by('store__name', '-date')

Leave a comment