简体   繁体   中英

Django query ~ Raw query must include the primary key

I try to make raw query in django but error

Raw query must include the primary key

this is query:

posts= User.objects.raw('select auth_group.name, auth_user.username 
                        from auth_user, auth_group, auth_user_groups 
                        where auth_user.id = auth_user_groups.user_id AND auth_group.id = auth_user_groups.group_id')

can you help me?

Following is a shell example. This may give a hint.
User and Group are in many-to-many relationship.

In [24]: from django.contrib.auth.models import User, Group

In [25]: user_id = 9038

In [26]: u = User.objects.get(id=user_id)

In [27]: u.username
Out[27]: u'parag@gmail.com'

In [28]: g = Group.objects.filter(user=u)

In [29]: g.all()
Out[29]: [<Group: Customer>, <Group: Admin>, <Group: Author>, <Group: Tester>, <Group: Report>]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM