Hello everybody!

I'm trying to extend the User model with extra fields that I need.
First I tried to inherit the User class and deregister the original
one, but the UserAdmin class, used to create new users, didn't
acknowledge my class and tried to redirect to an id that did not exist
yet. I figured it counted the original User class anyway. Never got
the middleware class to work either, which should in some way replace
the original User class in calls. Writing a custom authentication
backend worked though, so once having created a custom User, login
worked as well.

Thus switching to "the old way" (User profiles), due to lacking
documentation of above said scenario, I've gotten almost everything to
work. I've

 * Specified the fields I want included.
 * Made them appear under the auth.User admin (creating new Users and
giving passwords are no problems)

This is my setup:

class ModelBase(models.Model):
    created_by = models.ForeignKey(User, related_name="%(class)
sCreated", null=True, blank=True)
    created = models.DateTimeField(null=True, blank=True)

    updated_by = models.ForeignKey(User, related_name="%(class)
sUpdated", null=True, blank=True);
    updated = models.DateTimeField(null=True, blank=True)

This class is inherited by many classes. Generally there's been no
problem auto-filling its fields:

class MyModelAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        if not obj.id:
            obj.created = datetime.datetime.today()
            obj.created_by = request.user

        obj.updated = datetime.datetime.today()
        obj.updated_by = request.user

        obj.save()

This doesn't work with the User profile approach (it would work the
inheritance approach). I've tried various ways of getting it to work:

# Define an inline admin descriptor for UserProfile model
class UserProfileInline(admin.StackedInline):
    model = UserProfile
    fk_name = 'user'
    max_num = 1

# Define a new UserAdmin class
class MyUserAdmin(UserAdmin):
    inlines = [UserProfileInline, ]
    def save_formset(self, request, form, formset, change):
        for form in formset.forms:
            userProfile = form.save(commit=False)
            if not userProfile.id:
                userProfile.created = datetime.datetime.today()
                userProfile.created_by = request.user

            userProfile.updated = datetime.datetime.today()
            userProfile.updated_by = request.user

            userProfile.user = form.cleaned_data['user']

            userProfile.save()
        formset.save()


# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)

This makes no difference upon creating a new User. It should be noted
though, that upon resaving/updating an existing User, the updated(_by)
fields are updated.

Lastly, how would I go about setting list_display and search_fields
for the User admin referring to fields in the profile? And is there
any way of moving the profile fields so that they appear above the
Rights/Important dates/Groups sections in the admin page, since the
profile fields have more in common with Forename/Lastname etc.?

Cheers,
Jagot

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to