Search code examples
djangoadminmodels

Django, filter users by group in a model foreign key


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?


Solution

  • Use limit_choices_to paramether in your ForeignKey definition like this:

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