Answer the question
In order to leave comments, you need to log in
How to force Django to add GROUP BY to INNER JOIN via ORM?
Actually there is model.py (it doesn't matter which one) and there are a bunch of related tables in it (but everything is fine, without loops and duplication, everything is more or less normalized). And this is how the query looks like:
q=BlogPost.objects.\
order_by('-dPostDataCreate', 'kBlogAuthorUser__id').\
select_related()
print q.query
SELECT
`все-все-все-поля-из-всех-всех-всех-связанных-таблиц-через-запятую`
FROM prj_blogpost
INNER JOIN prj_ouruser ON ( prj_blogpost.kBlogAuthorUser_id = prj_ouruser.id )
INNER JOIN auth_user ON ( prj_ouruser.kDjangoUser_id = auth_user.id )
ORDER BY
prj_blogpost.dPostDataCreate DESC,
prj_blogpost.kBlogAuthorUser_id ASC
for cnt in q:
print cnt.username
Exception Value: 'BlogPost' object has no attribute 'username'
SELECT
`все-все-все-поля-из-всех-всех-всех-связанных-таблиц-через-запятую`
FROM prj_blogpost
INNER JOIN prj_ouruser ON ( prj_blogpost.kBlogAuthorUser_id = prj_ouruser.id )
INNER JOIN auth_user ON ( prj_ouruser.kDjangoUser_id = auth_user.id )
GROUP BY
`все-все-все-поля-из-всех-всех-всех-связанных-таблиц-через-запятую`
ORDER BY
prj_blogpost.dPostDataCreate DESC,
prj_blogpost.kBlogAuthorUser_id ASC
Answer the question
In order to leave comments, you need to log in
Sergey Eremin : you are killing me with the name of the variables in the sUserPhone camel case
and rewrite the prefixes
as it should, with underscores and without prefixes
and do not shorten the variable names
posts = BlogPost.objects.\
order_by('-dPostDataCreate', 'kBlogAuthorUser__id').\
select_related()
for post in posts:
print post.kBlogAuthorUser.kDjangoUser.username
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question