Django,在模型外键中按组过滤用户 [英] Django, filter users by group in a model foreign key

查看:35
本文介绍了Django,在模型外键中按组过滤用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个博客文章的模型,其中文章的所有者是用户的外键.使用该模型,任何用户都可以拥有博客文章.我想对其进行更改,以便只有特定组中的用户(我们称之为博主")才能拥有博客文章对象.理想情况下,它也应该出现在管理员中,我的意思是现在在博客文章管理员中,所有者"的菜单列出了所有用户,它应该只列出博主"组中的用户.我如何使用 Django 1.3 做到这一点?

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?

推荐答案

在您的 ForeignKey 定义中使用 limit_choices_to 参数,如下所示:

Use limit_choices_to paramether in your ForeignKey definition like this:

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

这篇关于Django,在模型外键中按组过滤用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆