filter staff user in combo box in django admin

Viewed 149

i want to show only staff user in combo box django admin how i most do it.

Article(models.Model):
    author = models.Foreignkey(User)
    ...
    # other fields

I want only staff user show in django admin combo box.

1 Answers

You can limit the choices with the limit_choices_to=… parameter [Django-doc]:

from django.conf import settings

Article(models.Model):
    author = models.Foreignkey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        limit_choices_to={'is_staff': True}
    )
    # …

This will filter the items if you use a ModelForm, or a ModelAdmin. This is however not enforced at the database, so if you make ORM calls, it is still possible to link the author to a non-staff member. But if you use forms, model admins, etc. properly it will thus only present Users with is_staff=True.


Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.

Related