Right way to create proxy to User model in Django 1.7

1,834 views
Skip to first unread message

Izabella Gál

unread,
Sep 12, 2014, 2:54:53 PM9/12/14
to django...@googlegroups.com
Hi!

Could you, please, help me to find the right way to create proxy to User
model in Django 1.7?

In 'myapp.models.py' I have a MyUser class which should inherit from the
user model specified in settings.AUTH_USER_MODEL:

If I am using the get_user_model() method, I receive the
'django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.'
exception

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

def my_method(self):
return 'hello'

And if I replace the get_user_model() method with
settings.AUTH_USER_MODEL, I receive another exception: TypeError: Error
when calling the metaclass bases
str() takes at most 1 argument (3 given)

In settings.py I have: AUTH_USER_MODEL = 'auth.User'

Thanks in advance!

Izabella

Daniel Roseman

unread,
Sep 13, 2014, 12:22:30 PM9/13/14
to django...@googlegroups.com, izabel...@vitheia.com
This doesn't make sense. AUTH_USER_MODEL should be set to your MyUser class, which in turn should inherit directly from auth.models.User. 
--
DR.

izabella.gal

unread,
Sep 15, 2014, 4:05:50 AM9/15/14
to django...@googlegroups.com, izabel...@vitheia.com
Thank you for your answer.
I corrected my code how you said: MyUser class inherits now directly from auth.models.User and in settings I set AUTH_USER_MODEL = 'myapp.MyUser' and I got another error: 

TypeError: MyUser cannot proxy the swapped model 'myapp.MyUser'.

I found this problem on stackoverflow (http://stackoverflow.com/questions/25144423/django-proxy-user-model-example) with one answer from you and tried to apply you suggestion: inherited from auth.models.AbstractUser instead of auth.models.User, but in this case I receive another error:

AttributeError: type object 'MyUser' has no attribute '_base_manager'

What am I doing wrong?

Thanks,
    Izabella

Andrew Pinkham

unread,
Sep 15, 2014, 11:51:11 AM9/15/14
to django...@googlegroups.com
Hi Izabella,
Could you provide your custom user model declaration?

According the the documentation, you should be inheriting from `AbstractBaseUser`.

https://docs.djangoproject.com/en/1.7/topics/auth/customizing/#substituting-a-custom-user-model

https://docs.djangoproject.com/en/1.7/topics/auth/customizing/#django.contrib.auth.models.AbstractBaseUser

JJ Zolper and Russel Keith Magee recently posted about a custom email user on the Django Developer mailing list. You may find the code useful:

https://github.com/freakboy3742/django/commit/f54da396a0be09550d69625e1215119dfb4898e4#diff-49fc6cea24d46bdb27339c1aab392e32R379

Note that the code inherits not only from `AbstractBaseUser`, but also from a `PermissionsMixin`, which may or may not be desirable for you.

Hope that helps,
Andrew

izabella.gal

unread,
Sep 16, 2014, 1:12:36 AM9/16/14
to django...@googlegroups.com, m...@andrewsforge.com
Hi Andrew,

My custom model looks like this:

from django.contrib.auth.models import User

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

    def my_custom_method(self):
        return "Something"

This is a proxy class and it's only scope is to extend the User model with a method. And how I understand from here: https://docs.djangoproject.com/en/1.7/topics/auth/customizing/#custom-users-and-proxy-models  I must inherit directly from the User model that is currently in use in my project.
I understood Daniel Roseman's suggestions and I wanted to follow it, but it thrown me the exception: TypeError: MyUser cannot proxy the swapped model 'myapp.MyUser'.

Collin Anderson

unread,
Sep 17, 2014, 11:01:51 AM9/17/14
to django...@googlegroups.com, m...@andrewsforge.com
Interesting yeah, why not create a "new" user model inheriting from AbstractBaseUser, and have it _not_ be a proxy model? It should have pretty much the same effect. You may need to say class Meta: db_table = 'auth_user'.

Reply all
Reply to author
Forward
0 new messages