Last active
July 29, 2018 14:30
-
-
Save ShinJJang/3bd949c390aabe79c1d91df1ed49bc8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ref: https://books.agiliq.com/projects/django-orm-cookbook/en/latest/ | |
# 8. How to filter a queryset with criteria based on comparing their field values | |
User.objects.filter(name_startswith='R') | |
User.objects.filter(last_name=F("first_name")) | |
User.objects.annotate(first=Substr("first_name", 1, 1), last=Substr("last_name", 1, 1)).filter(first=F("last")) | |
# 9. How to filter FileField without any file? | |
no_files_objects = MyModel.objects.filter( | |
Q(file='')|Q(file=None) | |
) | |
# 10. How to perform join operations in django ORM? | |
>>> a1 = Article.objects.select_related('reporter') // Using select_related | |
>>> a1 | |
<QuerySet [<Article: International News>, <Article: Local News>, <Article: Morning news>, <Article: Prime time>, <Article: Test Article>, <Article: Weather Report>]> | |
>>> print(a1.query) | |
SELECT "events_article"."id", "events_article"."headline", "events_article"."pub_date", "events_article"."reporter_id", "events_article"."slug", "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "events_article" INNER JOIN "auth_user" ON ("events_article"."reporter_id" = "auth_user"."id") ORDER BY "events_article"."headline" ASC | |
>>> a2 = Article.objects.filter(reporter__username='John') | |
>>> a2 | |
<QuerySet [<Article: International News>, <Article: Local News>, <Article: Prime time>, <Article: Test Article>, <Article: Weather Report>]> | |
>>> print(a2.query) | |
SELECT "events_article"."id", "events_article"."headline", "events_article"."pub_date", "events_article"."reporter_id", "events_article"."slug" FROM "events_article" INNER JOIN "auth_user" ON ("events_article"."reporter_id" = "auth_user"."id") WHERE "auth_user"."username" = John ORDER BY "events_article"."headline" ASC | |
# 11. How to find second largest record using Django ORM ? | |
>>> user = User.objects.order_by('-last_login')[1] // Second Highest record w.r.t 'last_login' | |
>>> user.first_name | |
'Raghu' | |
>>> user = User.objects.order_by('-last_login')[2] // Third Highest record w.r.t 'last_login' | |
>>> user.first_name | |
'Sohan' | |
SELECT "auth_user"."id", | |
"auth_user"."password", | |
"auth_user"."last_login", | |
"auth_user"."is_superuser", | |
"auth_user"."username", | |
"auth_user"."first_name", | |
"auth_user"."last_name", | |
"auth_user"."email", | |
"auth_user"."is_staff", | |
"auth_user"."is_active", | |
"auth_user"."date_joined" | |
FROM "auth_user" | |
ORDER BY "auth_user"."last_login" DESC | |
LIMIT 1 | |
OFFSET 2 | |
# 12. Find rows which have duplicate field values | |
>>> duplicates = User.objects.values( | |
'first_name' | |
).annotate(name_count=Count('first_name')).filter(name_count__gt=1) | |
>>> duplicates | |
<QuerySet [{'first_name': 'John', 'name_count': 3}]> | |
>>> records = User.objects.filter(first_name__in=[item['first_name'] for item in duplicates]) | |
>>> print([item.id for item in records]) | |
[2, 11, 13] | |
# 13. How to find distinct field values from queryset? | |
distinct = User.objects.values( | |
'first_name' | |
).annotate( | |
name_count=Count('first_name') | |
).filter(name_count=1) | |
records = User.objects.filter(first_name__in=[item['first_name'] for item in distinct]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment