Friday, October 1, 2010

Re: Change auth User display name in admin interface

You, sir, are awesome. It worked perfectly.

In case anyone else is wondering what I did:

In models.py:
# import the auth User
from django.contrib.auth.models import User

# Create the proxy model, inherits from User:
class myUser(User):
class Meta:
proxy = True

def __unicode__(self):
if not self.last_name or not self.first_name:
return u'%s' % (self.username)
else:
return u'%s, %s' % (self.last_name, self.first_name)

Also in models.py, all instances that had a ForeignKey or
ManyToManyField pointing to User, I changed to myUser.

In admin.py:
# import the auth User and UserAdmin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin

class MyModelAdmin(UserAdmin):
def formfield_for_manytomany(self, db_field, request, **kwargs):
if db_field.name == "users":
kwargs["queryset"] = myUser.objects.get(id=request.id)
return super(MyModelAdmin,
self).formfield_for_manytomany(db_field, request, **kwargs)

# Had to unregister the User so it could be registered with
MyModelAdmin
admin.site.unregister(User)
admin.site.register(User, MyModelAdmin)

Though to be completely honest, I have no idea what:

if db_field.name == "users":

is doing. I can change "users" to anything and it still works, but if
I take that line out, I cannot add a new User. So any help with that
would be appreciated. And if what I did can be done simpler or better,
please let me know.

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment