These are chat archives for django/django

Jan 2016
Jan 16 2016 13:03 UTC

@friendOfOurs : first of all I guess you are doing something wrong, if you want to get QuerySet of Users from Level, you should do it in different order


You will get all users whose level = 3, and probably Django ORM will save some queries for you by doing

SELECT users.* FROM  users u
INNER JOIN userprofile up ON up.user_id = 
WHERE up.level_id IN (SELECT FROM level WHERE id = 3)
Jan 16 2016 13:06 UTC
@ikhahmedov omg, that makes so much sense! Thanks! I was about to get rid of UserProfile model ^^ Thanks again!
Jan 16 2016 13:07 UTC
@friendOfOurs : welcome, by the way seems I made mistake

if filtering from User profile doesn't work, try to use UserProfile model then at the end add select_related


select_related decreases queries to db

Jan 16 2016 13:13 UTC
yep, looks good. but just out of curiosity: since I am going to be needing user's level a lot, maybe it wouln't be a bad idea to have the field directly in my auth user model?
Jan 16 2016 13:15 UTC
if you are using Django > 1.7 they have added AUTH_USER_MODEL setting so that you can inherit from User model and create your own Model, if you need level a lot, then in select_related() add 'level' --> select_related('user','level') than db will get only one query, otherwise you for each .level access ORM will send query to db see (N+1) problem in database theory
Jan 16 2016 13:22 UTC
(y) thanks
Jan 16 2016 19:04 UTC
thank you @bittner ...
you're very cool