views:

33

answers:

1

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!

A: 

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
Jordan Reiter
Thanks for your reply. I have tried several ways, but I cannot seem to get fields from the User and UserProfile in one form. As far as I know this is impossible. Do you mean you use `class Meta: model=UserProfile` and manually add the User fields (these fields will not actually be connected to the User model).Could you please explain in a little more detail how you would do this?
Heyl1
Yeah, as far as I know there's no way to do this automatically. But a combination of manually putting in the fields and then using `initial` to fill-in the forms, and again, using a custom `save` function to handle saving the User record later. For your particular case, you might look at this: http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/
Jordan Reiter
Jordan, thanks again for your reply. Sorry for the late response. I've had a look at the link and I've tested it. What wouldn't work for me is setting the password, because the link to the password create page is incorrect. You have to go to the actual user instance and do it there (so User has to be in the admin). Don't know if you have any experience with this. I think the solution is pretty neat, but I'm a little hesitant about not using the user_profile (and there's some critique in the comments). Your other suggestion is less intuitive though so I think I'll go with the link's solution.
Heyl1