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

查看:277
本文介绍了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?

推荐答案

使用 limit_choices_to 参数你的ForeignKey定义如下:

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天全站免登陆