0

I would like to create a User model that has an additional couple fields in django. I tried to do so by following the advice https://stackoverflow.com/a/22696794/3426600 to create a custom user model in models.py

from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
    user_data = models.TextField(null=True)

In settings.py

AUTH_USER_MODEL = 'app_name.User'

but when I run python manage.py syncdb

I get the errors:

CommandError: One or more models did not validate: account.emailaddress: 'user' has a relation with model app_name.User, which has either not been installed or is abstract. admin.logentry: 'user' has a relation with model app_name.User, which has either not been installed or is abstract. auth.user: Model has been swapped out for 'app_name.User' which has not been installed or is abstract. socialaccount.socialaccount: 'user' has a relation with model app_name.User, which has either not been installed or is abstract.

Could I be using the wrong app_name? If so, where do I find the app_name? How do I make the custom user work with socialaccount, etc?

Community
  • 1
  • 1
ninajay
  • 527
  • 1
  • 5
  • 10

1 Answers1

1

Assume your project strucutre is something like below, and the codes of User(AbstractUser) are located in mysite/mysite/app1/models.py:

mysite/    (root folder for the project, put it anywhere in your disk. Most developers have a workspace folder(s) in computer)
    (other non-application things. E.g. a static/, README.md, ...)
    manage.py
    mysite/    (container for all applications. You can put applications directly under root folder, but not recommended)
        __init__.py
        settings.py
        urls.py
        wsgi.py
        app1/     (each application resides in its own folder. You don't want to put all models in one models.py file or all views in one view.py file, right?)
            __init__.py
            models.py
            views.py
            ......

Then the app_name is app1, i.e. AUTH_USER_MODEL = 'app1.User'. Meanwhile, you need to add mysite.app1 into INSTALLED_APPS in settings.py. This probably will solve you CommandError issue.

ADDED NOTES:

Each application, you can consider it as a module of your project. The app_name of the application is the folder name. If you have defined models in one application, must add it into INSTALLED_APPS in settings.py.

Manually creating a folder is not a good way. django-admin.py startapp is more recommended, because it together creates some common files for an application, e.g. init.py, models.py, view.py, ...

Suggest you to go through the django quick-start guide, if you haven't done it: https://docs.djangoproject.com/en/dev/intro/tutorial01/

ZZY
  • 3,689
  • 19
  • 22
  • Thanks so much for the response--the models.py is actually inside of my mysite/ folder. Is this wrong? Should I make another app1 folder within the mysite/ folder? Sorry, are these physical folders in a directory or project structures from python/django? – ninajay May 24 '14 at 04:35
  • I added some notes for django project structure and so on, in original response – ZZY May 24 '14 at 07:08
  • Cool--I read the documentation and added a users module and syncdb works and I can log in with facebook but on the callback, I get the error that "User matching query does not exist." http://stackoverflow.com/questions/24066914/custom-users-with-django-allauth – ninajay Jun 05 '14 at 23:16