Error while customising Django User

55 views
Skip to first unread message

Web Architect

unread,
Dec 18, 2015, 11:55:16 AM12/18/15
to Django users
Hi,

I am new to Django and still under the process of learning. We are using an open source ecommerce platform - Oscar - for our online store. Oscar is based on Django.

I was trying to customise the Django User model by extending with some few extra fields/columns:

# file: your-project/apps/user/models.py
from django.db import models

from django.contrib.auth.models import AbstractUser
class User(AbstractUser): user_type = models.IntegerField()
# use our own user model in settings.py
AUTH_USER_MODEL = "user.User"
With the above changes, when I run 'makemigration' followed by 'migrate', I am getting the following errors:

Running migrations:

  Rendering model states...Traceback (most recent call last):

  File "./manage.py", line 10, in <module>

    execute_from_command_line(sys.argv)

  File "/Users/pinakeebiswas/waltzz/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line

    utility.execute()

  File "/Users/pinakeebiswas/waltzz/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute

    self.fetch_command(subcommand).run_from_argv(self.argv)

  File "/Users/pinakeebiswas/waltzz/lib/python2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv

    self.execute(*args, **cmd_options)

  File "/Users/pinakeebiswas/waltzz/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute

    output = self.handle(*args, **options)

  File "/Users/pinakeebiswas/waltzz/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 222, in handle

    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)

  File "/Users/pinakeebiswas/waltzz/lib/python2.7/site-packages/django/db/migrations/executor.py", line 100, in migrate

    state.apps  # Render all real_apps -- performance critical

  File "/Users/pinakeebiswas/waltzz/lib/python2.7/site-packages/django/utils/functional.py", line 60, in __get__

    res = instance.__dict__[self.name] = self.func(instance)

  File "/Users/pinakeebiswas/waltzz/lib/python2.7/site-packages/django/db/migrations/state.py", line 166, in apps

    return StateApps(self.real_apps, self.models)

  File "/Users/pinakeebiswas/waltzz/lib/python2.7/site-packages/django/db/migrations/state.py", line 248, in __init__

    raise ValueError(msg.format(field=operations[0][1], model=lookup_model))

ValueError: Lookup failed for model referenced by field basket.Basket.owner: customer.User


I am not sure what's causing the above error. Certainly there could be models which would be dependent on the User model but I thought the makemigrations and migrate should be able to resolve those.
Would appreciate if someone could help me with resolving the above errors.
Thanks


    

Tim Graham

unread,
Dec 18, 2015, 2:54:57 PM12/18/15
to Django users
If you didn't start your project with a custom user, it's non-trivial to start using one. Please see https://code.djangoproject.com/ticket/25313.

Web Architect

unread,
Dec 19, 2015, 10:12:52 AM12/19/15
to Django users
Hi Tim,

Thanks for your response and the details.

In that case, what would be the best approach to have additional credentials for the user? Do I need to create another model/table and link it to User? In that case, how will that get included in the User object? Do I need to create a middleware?

I am sorry if some queries seem out of the way as I am new to Django. 

Thanks.

Tim Graham

unread,
Dec 19, 2015, 3:56:37 PM12/19/15
to Django users

Abdoul Aziz Abdoulaye

unread,
Dec 19, 2015, 4:41:48 PM12/19/15
to Django users
Hello,
You can use django monkey patching for adding additional fields to User model

for your case it will look like this.

User.add_to_class('user_type', model.IntergerField())

Web Architect

unread,
Dec 22, 2015, 3:11:47 AM12/22/15
to Django users
Hi Tim,

Thanks for your suggestion. Will go through the documentation and will try it out.

Thanks.

Web Architect

unread,
Dec 22, 2015, 3:12:23 AM12/22/15
to Django users
Hi,

Thanks for your suggestion. Will try it out.

Thanks.
Reply all
Reply to author
Forward
0 new messages