Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 400
Bastian Avatar asked Jun 20 '12 11:06

Bastian


1 Answers

Use limit_choices_to paramether in your ForeignKey definition like this:

author = models.ForeignKey("auth.User", limit_choices_to={'groups__name': "bloggers"})
like image 193
Aidas Bendoraitis Avatar answered Sep 30 '22 13:09

Aidas Bendoraitis