Django, filter users by group in a model foreign key

雨燕双飞 提交于 2019-12-04 13:40:33

问题


I have a model for a blog post where the owner of the post is a foreign key to User. With that model any user can own a blog post. I would like to change it so that only the users in a certain group -let's call it 'bloggers'- can own a blog post object. Ideally it should appear in the admin too, I mean in the blog post admin right now the menu for 'owner' lists all the users, it should only list the ones in the 'bloggers' group. How do I do that with Django 1.3?


回答1:


Use limit_choices_to paramether in your ForeignKey definition like this:

author = models.ForeignKey("auth.User", limit_choices_to={'groups__name': "bloggers"})


来源:https://stackoverflow.com/questions/11118179/django-filter-users-by-group-in-a-model-foreign-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!