Django queryset filtering

91 views
Skip to first unread message

mtp...@gmail.com

unread,
Jun 24, 2020, 6:25:18 PM6/24/20
to Django users
I have a queryset that returns all the expired licenses objects as shown below.

qs = LicenseIssue.objects.filter(expiry_date__lte=TODAY).order_by('-expiry_date')    

Here is my situation:
There exists multiple issued licenses of different businesses for year 2020 and their  previous issued licenses for the for year 2019, 2018 and so on. The above queryset results to listing all expired licenses though I'd like to make it possible if there exists issued licenses for year 2020 their previous issued licenses should not appear in the expired licenses queryset. Please advise on this.

Oleg Kishenkov

unread,
Jun 25, 2020, 6:43:26 PM6/25/20
to django...@googlegroups.com
Hello, use a filtering chain, a refined queryset is itself a queryset so you can filter it further. Also, Django has extensive lookup support in its filtering methods. Let's assume LicenseIssue is related to Business as many to one (it is probably the case):

class Business(models.Model):
    name = models.CharField(max_length=15)

class LicenseIssue(models.Model):
    expiry_date = model.DateField(null=False)
    business = model.ForeignKey(Business, on_delete=models.CASCADE)

First you filter out expired licenses, second you exclude the licences which belong to businesses that have valid licences.

qs = LicenceIssue.objects.filter(expiry_date__lte=datetime.now()).exclude(business__license__expiry_date__gte='2020-01-01')

I have to point out that here license in business__license__expiry_date__gte is an automatically generated related_query_name.

Oleg

чт, 25 июн. 2020 г. в 01:25, mtp...@gmail.com <mtp...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7d5ed088-c21b-4bd3-aba3-0f61c05efa50n%40googlegroups.com.

Dan Madere

unread,
Jun 26, 2020, 11:27:23 AM6/26/20
to Django users
I'd make two queries. One to see which businesses currently have active licenses, then a second to get the expired licenses, excluding the businesses from the first query. Here's some example code, assuming the LicenseIssue model has a "business" foreign key field:

active_business_ids = LicenseIssue.objects.filter(expiry_date__gt=TODAY).values_list('business_id', flat=True).distinct()
expired_licenses = LicenseIssue.objects.filter(expiry_date__lte=TODAY).exclude(business_id__in=active_business_ids).order_by('-expiry_date')

mtp...@gmail.com

unread,
Oct 6, 2020, 1:35:00 PM10/6/20
to Django users
Thanks guys this was very useful 

Dvs Khamele

unread,
Oct 11, 2020, 1:07:58 PM10/11/20
to django...@googlegroups.com
Hi do you hire contract based python/django freelancer?
 We can help you in this and related tasks at fair prices. Reply or send email to div...@pythonmate.com
Best Regards, 
Divyesh Khamele,
Pythonmate

Reply all
Reply to author
Forward
0 new messages