Search code examples
pythondjangodjango-admindjango-1.9

Do you and should you rename a custom User model in Django 1.9?


I am creating a new User model for my Django project. I have been many people calling their custom user model, XXXUser and custom user manager, XXXUserManager.

I was wondering if there is a reason for this. Can you just create a custom user and still call it User? Does this create conflicts in the code?


Solution

  • Basically you can. But for readability purposes it's better to do it XxxUser, if you see XxxUser you are instantly understand that this is custom one. And you need to keep in mind that you should replace some code that is common for base usage.

    Such as(should be replaces)

    from django.contrib.auth.models import User
    

    Should be

    from django.contrib.auth import get_user_model
    User = get_user_model()
    

    And if you reference to User model in your models.py you need to

    from django.conf import settings
    
    class SomeModel(models.Model):
        field = models.RetationField(settings.AUTH_USER_MODEL)
    

    Also do not forget to set your settings.AUTH_USER_MODEL that references to your custom one