Python – Overrides the default user model methods

Overrides the default user model methods… here is a solution to the problem.

Overrides the default user model methods

I’ve been trying to override the default __unicode__() method of the django.contrib.auth.models user model, but I can’t get it to work.

I’ve tried this:

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

class User(models. Model):
        def __unicode__(self):
            return "pie"

and

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

class User(User):
        def __unicode__(self):
            return "pie"

But it doesn’t work, I know

it’s wrong to do this, but I don’t know how to do it properly.

What I want it to do is say “pie” instead of the username in the admin panel.

Edit:

Trying to do just that:

class MyUser(User):
    class Meta:
        proxy = True

def __unicode__(self):
        if self.get_full_name() == '':
            return "pie"
        else:
            return self.get_full_name()

I used the MyUser class instead of User when referencing ForeignKey.

Solution

You might want to check out Django’s proxy model concept. They even showed an example of using User as the base class.

On the other hand, if you try to change the actual __unicode__() method in the actual User class, you may have to MonkeyPatch it. It’s not difficult, but I’ll leave the specifics to you as a learning experience.

Related Problems and Solutions