djangomodel

Having models directory and AUTH_USER_MODEL


I have myApp/models/profiles.py instead of myApp/models.py (to group related models)

How do you set AUTH_USER_MODEL in this case?

Because auth accepts only "foo.bar" pattern.

app_label, model_name = settings.AUTH_USER_MODEL.split('.')

Solution

  • Django expects the models for any given app to be in app.models. If you want to use this kind of file structure, you'll need to still make sure this is the case. The easiest way to do this is too add from profiles import * in myApp/models/__init__.py and then use AUTH_USER_MODEL as normal.

    For example, you you had myApp/models/profiles.py and myApp/models/actions.py your myApp/models/__init__.py should read

    from profiles import *
    from actions import *
    

    Remember to make sure you don't have any name conflicts too, and you may wish to use set your __all__ value in each of your sub-packages.