Question

I'm extending a the auth.models.User but I'm having troubles to implement the __unicode__ methode.

from django.db import models
from django.contrib.auth.models import User

class Artist(models.Model):
    user = models.OneToOneField(User)
    city = models.CharField(max_length=30)
    bio = models.CharField(max_length=500)

Now how to I access the user fields as I want to return the name for Django Admin.

Was it helpful?

Solution

class Artist(models.Model):
    # fields

    def __unicode__(self):
        return "{first_name} {last_name}".format(
            **dict(
                first_name=self.user.first_name,
                last_name=self.user.last_name
            )
        )

Though User already has a function to concat the name fields, so this will work too:

def __unicode__(self):
    return "{0}".format(self.user.get_full_name())

Or even

def __unicode__(self):
    return self.user.get_full_name()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top