If you're using the most recent version of django you can use a Proxy
Model:
(I think this will work but I've never tried it)
class Author(User):
    class Meta:
        proxy = True

    #Here you redefine the default behavior of
    #of the __unicode__ function
    def __unicode__(self):
        author = ("%s %s" % (self.first_name,self.last_name)).strip()
        if len(author)> 0: return author
        return self.username
                
        ...
class BlogPost(models.Model):
    title = models.CharField(max_length=150)
    body = models.TextField()
    author = models.ForeignKey(Author)
    timestamp = models.DateTimeField()


If not just write a custom getAuthor() method:

class BlogPost(models.Model):
    title = models.CharField(max_length=150)
    body = models.TextField()
    author = models.ForeignKey(User)
    timestamp = models.DateTimeField()

    def getAuthor(self):
        theauthor = ("%s %s" %
(self.author.first_name,self.author.last_name)).strip()
        if len(theauthor)> 0: return theauthor
        return str(self.author)


W

-----Original Message-----
From: django-users@googlegroups.com
[mailto:django-us...@googlegroups.com] On Behalf Of Thierry
Sent: Sunday, May 17, 2009 4:59 PM
To: Django users
Subject: How do I override __unicode__ for User?


I currently have a blog model:

class BlogPost(models.Model):
    title = models.CharField(max_length=150)
    body = models.TextField()
    author = models.ForeignKey(User)
    timestamp = models.DateTimeField()

Right now, author is returning the default User.username.  I have
other models who has a ForeignKey to User, I want them to keep
defaulting to username while for Blogpost, I want it to return
"first_name last_name".  How can I customize the above so that author
returns me "first_name last_name" if they are present?


--~--~---------~--~----~------------~-------~--~----~
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