Add UserProfile Inline in the change_View function

42 views
Skip to first unread message

bvcelari

unread,
May 9, 2012, 5:43:15 PM5/9/12
to django...@googlegroups.com
Hello,
I´m trying to custom admin in django 1.4. I went to docs, followed the instructions, and worked.
The issue comes when I try to modify change_view, and find that inlines does not work inside change_view function. My target is be able to allow users to change only their username (from User model)
and their Profile information, but allow Admin user to access all the info.

My UserProfile model like this:
##models.py
##my smple profile
class UserProfile(models.Model):
   user=models.OneToOneField(User)
   extra_field=models.Charfield(max_length=100,blank=True)

My admin:
##admin.py
class UserProfileInline(admin.StackedInline):
   model = UserProfile

class UserAdmin(admin.ModelAdmin):
   ##If I  add inlines here, should work, but only for admin user...
   ##inlines = [UserProfileInline]
   def change_view(self,reques,object_id):
      try:
         if request.user.is_superuser:
            ##here works for admin user
            self.inlines = [UserProfileInline]
         else:
            ##here I defined fieldsets, so, users only can modify its own name
            self.fieldsets =(
                 (None, {'fields':('username')}
            )
            ##I thought that here, shoould add Profile fields to... but did not work
            self.inlines = [UserProfileInline]

         return super(UserAdmin,self).change_view(request,object_id)
      except:
         raise PermissionDenied

Perhaps I miss something simple, or just did not work as expected...
any tips about how to achieve my target will be appreciate.
Thank you very much
       

Reply all
Reply to author
Forward
0 new messages