2017-12-11 5 views
0

私はDjangoとそのORMの初心者です。複数のフィールドを数え、django ormを使ってグループ化するには

Userには多くのユーザーがいます。 、これは、ようなことができ、私はテーブルからグループのアクティブユーザーと総 ユーザーに必要とも私は彼らがSQLでDjangoのORM を使用して参加した年に基づいて、それらを注文する必要が

select count('users'), 
count('users' where is_active is True as 'active_users') form user 
order by 'created_at' 

答えて

0
# Get all users include active users 
users = User.objects.all().order_by('created_at') 
users_count = users.count() 
# Get only active users 
active_users = User.objects.filter(is_active=True).order_by('created_at') 
active_users_count = active_users.count() 
表集計用として

から

グループのアクティブユーザ及び全ユーザは、このanswer

参照します
関連する問題