3

I am having problems with my custom user model while using Django 1.7.1 and Python 3.4. I have declared a Custom user model in an apps.users.AuthUser. I then have another application (apps.pets) that will use the AuthUser as a ForeignKey in a Pet model. See below:

class Pet(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, db_index=True, blank=False, null=False)

This is my scenario:

In my INSTALLED_APPS I have: "apps.users"

If I set AUTH_USER_MODEL="apps.users.AuthUser" the exception is raised when I run 'runserver':

File "/home/frank/.virtualenvs/myproj/lib/python3.4/site-packages/django/contrib/auth/checks.py", line 12, in check_user_model
    cls = apps.get_model(settings.AUTH_USER_MODEL)
  File "/home/frank/.virtualenvs/myproj/lib/python3.4/site-packages/django/apps/registry.py", line 201, in get_model
    app_label, model_name = app_label.split('.')
ValueError: too many values to unpack (expected 2)

If I set AUTH_USER_MODEL="users.AuthUser" the exception is raised when I run migrate:

File "/home/frank/.virtualenvs/myproj/lib/python3.4/site-packages/django/db/migrations/state.py", line 89, in render
    model=lookup_model,
ValueError: Lookup failed for model referenced by field pets.Pet.owner: users.AuthUser

Similar issue reported here I guess: https://code.djangoproject.com/ticket/19845

Does this mean that in AUTH_USR_MODEL setting I have to use 'app_label.model_name' instead of 'apps.app_label.model_name.'? Is there a workaround for this?

EDIT1: My project structure is as follows:

SITE_ROOT
|-- PROJECT_ROOT/
|   |-- apps/
|   |   |-- app1
|   |   |-- app2
|   |-- etc/
|   |-- libs/
|   |-- media/
|   |-- requirements/
|   |   |-- __init__.py
|   |   |-- common.py
|   |   |-- development.py
|   |   |-- production.py
|   |   `-- staging.py
|   |-- settings/
|   |   |-- __init__.py
|   |   |-- common.py
|   |   |-- development.py
|   |   |-- production.py
|   |   `-- staging.py
|   |-- templates/
|   |-- README
|   |-- __init__.py
|   |-- .gitignore
|   `-- manage.py
Frankline
  • 40,277
  • 8
  • 44
  • 75
  • 1
    `AUTH_USER_MODEL` should be `users.AuthUser`, given that `AuthUser` lives in a `models.py` file in your `users` app. The `ValueError` is most likely the result of another error in your code, but I can't really give you specific pointers. – knbk Oct 27 '14 at 14:24
  • Can you add your project structure? – cdvv7788 Oct 27 '14 at 18:36
  • @cdvv7788 Why did you request to add project structure without any responsibility? – LKM Jul 13 '16 at 15:13

1 Answers1

0

In your installed apps you can have just your application name assuming its in the same directory, no need to specifically mention 'myapp.users' just 'myapp' will do.

The use this:

AUTH_USER_MODEL = 'myapp.MyAuthUserModel'

Don't use this:

AUTH_USER_MODEL = 'myapp.models.MyAuthUserModel'

Hope this helps!

Satchel
  • 57
  • 9