Getting a widgets for custom user property in the admin view

In my models.py I have those classes:

class Team(models.Model):
    name = models.CharField(max_length=100)

class User(AbstractUser):
    team = models.ForeignKey(Team, on_delete=models.PROTECT, null=True)

In my admin.py I have:

admin.site.register(User, UserAdmin)

In the admin view I can assign roles to the users, change their password, … but don’t get a widget for selecting a team. According to Google this seems to be a common question, so I found way too many different answers which seem to vary a lot between Django versions, which is quite confusing for a newbie.

I would like to have drop down on the users page that allows me to select an existing team. Can somebody give me a hint what’s the simplest way to get this in Django 5.1?

As far as I know, the default widget for foreign keys is a dropdown.
How does it come out now? Add a screenshot.

I don’t get a widget at all. I see a form with just the fields of a default user. My custom fields are ignored. When searching for the problem I find links like Custom user: how to change the admin forms? My problem is, that I’m rather new to the topic and I found various very specific workarounds for different Django versions.

So my question is how to get it working in the simplest possible way. I can probably follow the solution from the above link and create custom forms, but I would like to understand whether that’s the preferred way, whether there is a simpler solution or whether I’m completely off with my approach.

Did you start your project with your custom user model, or is this something you’ve added after the project was started? (If you added this after the project was already started, see Changing to a custom user model mid-project.)

Also post your UserAdmin model.

1 Like

I started the project with my custom user model and I use this UserAdmin:

from django.contrib.auth.admin import UserAdmin

Does your question imply that I have to customize the UserAdmin? That might be the source of my confusion: I assumed that UserAdmin is using the generated forms for my custom user model, so it should include all fields automatically. Based on my very positive experience with Django so far I assumed that it would “just work” and don’t see what I need to customize.

Yes.

The system-provided UserAdmin model is specific to the system-provided User model. You would, at a minimum, need to subclass the UserAdmin class, adding the team field to the fieldsets entry.

No, this is not a valid assumption. The UserAdmin class is a bit of a strange beast in that it behaves differently depending upon whether you are adding a new user or modifying an existing user. It also does not directly provide a “password-change” field, instead requiring a separate entry for that.

I suggest you look at UserAdmin, UserCreationForm, and UserChangeForm to see how they are constructed.

1 Like

Thanks a lot! Everything makes much more sense now and I’m optimistic that I’ll be able to figure out the details.