Changing self display of User in Django
I would like to see the user.email instead of the user.username when print(user) is called. This is to say that in my admin, i would like to see the emails as foreign keys.
Normally i would do in the following way as described on the django tutorial:
class Poll(models.Model):
# ...
def __unicode__(self):
return self.question
However, User class is prewritten and i don't want to mod Django. How then should i proceed?
UPDATE: I added the following to my Model:
def email(self):
u = User.object.get(pk=self.user.id)
return u.e开发者_Python百科mail
How do i tie it to my list_display now?
You could define a method on your Poll class called 'get_username' or something, that returns the email address of the user instead of their actual username. Then pass 'get_username' as a parameter to your 'list_display' attribute in the ModelAdmin of your Poll class.
The use-case you define requires overriding the User.__unicode__
method.
From the django docs on list_display:
If the field is a ForeignKey, Django will display the
__unicode__()
of the related object.
I can't see any way around this.
精彩评论