Question

I have a User Profile which is currently shown in the Admin via a Stacked Inline. However because I have fields such as last_name_prefix and last_name_suffix (for foreign names such as Piet van Dijk to cover proper sorting by last name) I would like to be able interleave the user profile fields with the normal change user fields. So in the Change User admin interface it would appear like this:

First Name:
Last Name Prefix:
Last Name
Last Name Suffix:

I have tried this solution: http://groups.google.com/group/django-users/browse_thread/thread/bf7f2a0576e4afd1/5e3c1e98c0c2a5b1. But that just created extra fields in the user form that weren't actually coming from the user profile (they stayed empty even though they should get values from the user profile).

Could someone explain to me if this could be done and how? Thanks very much!

Was it helpful?

Solution 2

This question has been solved by the new Django version 1.5: https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#auth-custom-user.

OTHER TIPS

I'm pretty sure you'd need to overwrite normal User admin.

What I would actually do is create a special forms.ModelForm for UserProfile called, say UserProfileAdminForm which included fields from the User model as well. Then you'd register UserProfile for admin and the save function for the UserProfileAdminForm would capture the user-specific fields and either create or update the User record (This is left as an exercise to the OP).

More info

When I say add more fields to a form, I mean manually add them:

class UserProfileAdminForm(forms.ModelForm):
    username = forms.CharField(...)
    email = forms.EmailField(...)
    first_name = ...
    last_name = ...

    def __init__(self, *args, **kwargs):
        super(UserProfileAdminForm, self).__init__(*args, **kwargs)
        profile = kwargs.get('instance', None)
        if profile and profile.user:
            self.user = profile.user
            self.fields['username'].initial = self.user.username
            self.fields['last_name'].initial = ...
            ...

    class Meta:
        model = UserProfile
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top