dangerChihuahua007
dangerChihuahua007

Reputation: 20895

Why does the string representation of User objects remain the same in my Django application?

I altered the User class in "/home/david/django/django/contrib/auth/models.py" as follows to override the string representation for a user in my Django application.

class User(models.Model):
    ...
    def __unicode__(self):
        return self.get_profile().full_name()

I had written a function called full_name() in my user profile model to display full names the way I want them to be displayed.

However, after I restart Apache, I find that users in select menus of model forms are still represented by usernames. Why?

enter image description here

Upvotes: 0

Views: 744

Answers (1)

Maccesch
Maccesch

Reputation: 2128

Don't try to monkey patch you installation. It really is a bad idea.

You can read here how you can override the way a model choice form field shows its model instances.

In you case it would look something like this:

class UserChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.get_profile().full_name()

Then use this field in your forms. In a model form you will have to override the default field that is used.

Upvotes: 2

Related Questions