How to display users who are only included in the Group in django admin panel

I have an Author simple models in my Django project as below:

class Author(models.Model):
    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, default=None)

    def __str__(self):
        return self.user.username

note: CustomUser is django's default User model which I added with a phone number

In the Group models (in Django admin panel), I have created 'Author' group, to separate or differentiate ordinary users or authors.

I want when I enter a new author name in the Author model, the only names that appear are those that belong to the 'Author' group.

You can work with limit_choices_to=… [Django-doc]:

class Author(models.Model):
    user = models.OneToOneField(
        CustomUser,
        on_delete=models.CASCADE,
        default=None,
        limit_choices_to={'groups__name': 'Author'},
    )

    def __str__(self):
        return self.user.username
Back to Top