Limit ForeignKey to Staff Users in Django

Django Logo If you've ever built a site that has different kinds of users, you've probably run across the case where you want a limited number of users to appear in a particular list. For example, you may only want staff members to be assigned ownership of a particular object. If your site has a really huge user list, combing through the users to find staff members can get annoying fast.

The key to limiting the choices to staff members only is the limit_choices_to argument to the model ForeignKey field. For example:

class Computer(models.Model):
  admin = models.ForeignKey(User, limit_choices_to={'is_staff': True})

In the above example, objects in our Computer class have an administrator that must be a staff user. This separates them from other users in the company that may not be staff members.

My Bookshelf

Reading Now

Other Stuff