Q:

Django Custom User is not showing a extra field with foreign key as Select list

I have a Custom User
class CustomUser(AbstractUser):
    registration_code = models.ForeignKey(RegistrationCode, null=True)
    ...

    def __str__(self):
        return self.username

class RegistrationCode(models.Model):
    code = models.CharField(max_length=30, null=True, blank=True)

    def __str__(self):
        return self.code

Within the admin page I would like to be able to set the "registration_code".
class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = CustomUser

    list_display = ['email', 'username', 'registration_code']

    fieldsets = (
        (None, {
            'fields': ('email', 'username', 'registration_code')
        }),
    )

admin.site.register(CustomUser, CustomUserAdmin)

But on the admin page I do not get a select list. But a default input field:

And if I edit the registration_code and try to save I get:
Cannot assign "'2'": "CustomUser.registration_code" must be a "RegistrationCode" instance.

Which sounds logical because I need to enter a RegistrationCode instance.
The same construction works for other models but for a Custom User it is not?? This is driving me crazy. Anybody an idea why I do not get the Select list?

A:

Django handle foreign key by model instance not assignment. So the problem come from your CustomUserChangeForm where registratio_code must be ModelChoiceField

