Home > Blockchain >  Troubles extending from Django-Oscar AbstractUser model
Troubles extending from Django-Oscar AbstractUser model

Time:10-30

I've been trying to fork django-oscar customer app. I've followed the guidelines on their documentation, but I can not apply migrations due to a ValueError: Related model 'customer.user' cannot be resolved. My project directory looks like this:


project
├── config
│   ├── asgi.py
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── customer
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0002_auto_20150807_1725.py
│   │   ├── 0003_update_email_length.py
│   │   ├── 0004_email_save.py
│   │   ├── 0005_auto_20181115_1953.py
│   │   ├── 0006_auto_20190430_1736.py
│   │   ├── 0007_auto_20200801_0817.py
│   │   ├── 0008_alter_productalert_id.py
│   │   ├── 0009_user.py
│   │   ├── __init__.py
│   ├── models.py
├── manage.py

Here are the steps I followed to arrive at the point I am now:

  • ./manage.py oscar_fork_app customer .
  • I added the customer app to my list of INSTALLED_APPS

DJANGO_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django.contrib.sites",
    "django.contrib.flatpages",
]

PROJECT_APPS = [
    "customer",
    # "customer.apps.CustomerConfig",,
]

OSCAR_APPS = [
    "oscar.config.Shop",
    "oscar.apps.analytics.apps.AnalyticsConfig",
    "oscar.apps.checkout.apps.CheckoutConfig",
    "oscar.apps.address.apps.AddressConfig",
    "oscar.apps.shipping.apps.ShippingConfig",
    "oscar.apps.catalogue.apps.CatalogueConfig",
    "oscar.apps.catalogue.reviews.apps.CatalogueReviewsConfig",
    "oscar.apps.communication.apps.CommunicationConfig",
    "oscar.apps.partner.apps.PartnerConfig",
    "oscar.apps.basket.apps.BasketConfig",
    "oscar.apps.payment.apps.PaymentConfig",
    "oscar.apps.offer.apps.OfferConfig",
    "oscar.apps.order.apps.OrderConfig",
    # "oscar.apps.customer.apps.CustomerConfig",
    "oscar.apps.search.apps.SearchConfig",
    "oscar.apps.voucher.apps.VoucherConfig",
    "oscar.apps.wishlists.apps.WishlistsConfig",
    "oscar.apps.dashboard.apps.DashboardConfig",
    "oscar.apps.dashboard.reports.apps.ReportsDashboardConfig",
    "oscar.apps.dashboard.users.apps.UsersDashboardConfig",
    "oscar.apps.dashboard.orders.apps.OrdersDashboardConfig",
    "oscar.apps.dashboard.catalogue.apps.CatalogueDashboardConfig",
    "oscar.apps.dashboard.offers.apps.OffersDashboardConfig",
    "oscar.apps.dashboard.partners.apps.PartnersDashboardConfig",
    "oscar.apps.dashboard.pages.apps.PagesDashboardConfig",
    "oscar.apps.dashboard.ranges.apps.RangesDashboardConfig",
    "oscar.apps.dashboard.reviews.apps.ReviewsDashboardConfig",
    "oscar.apps.dashboard.vouchers.apps.VouchersDashboardConfig",
    "oscar.apps.dashboard.communications.apps.CommunicationsDashboardConfig",
    "oscar.apps.dashboard.shipping.apps.ShippingDashboardConfig",
]

INSTALLED_APPS = DJANGO_APPS   PROJECT_APPS   OSCAR_APPS

As you can see I tried the "oscar" way of addind the app by specifying the path to the CustomerConfig method they generate with their command but it did not help, so I tried the regular "django" way and it did not work either.

  • And then I added my new model to the AUTH_USER_MODEL settings keys like this:

AUTH_USER_MODEL = "customer.User"

When I try to make migrations, it works as intended, and creates the migration number 9 in the list you can see above, but when I try to apply the migrations, I get this error:


./manage.py migrate
Operations to perform:
Apply all migrations: address, admin, analytics, auth, basket, catalogue, communication, contenttypes, customer, flatpages, offer, order, partner, payment, reviews, sessions, shipping, sites, thumbnail, voucher, wishlists
Running migrations:
Applying contenttypes.0001_initial... OK
Applying catalogue.0001_initial... OK
Applying customer.0001_initial...Traceback (most recent call last):
File "./manage.py", line 22, in <module>
    main()
File "./manage.py", line 18, in main
    execute_from_command_line(sys.argv)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/db/migrations/migration.py", line 126, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/db/migrations/operations/models.py", line 92, in database_forwards
    schema_editor.create_model(model)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 343, in create_model
    sql, params = self.table_sql(model)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 162, in table_sql
    definition, extra_params = self.column_sql(model, field)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 215, in column_sql
    db_params = field.db_parameters(connection=self.connection)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/db/models/fields/related.py", line 1004, in db_parameters
    return {"type": self.db_type(connection), "check": self.db_check(connection)}
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/db/models/fields/related.py", line 1001, in db_type
    return self.target_field.rel_db_type(connection=connection)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/db/models/fields/related.py", line 897, in target_field
    return self.foreign_related_fields[0]
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/db/models/fields/related.py", line 644, in foreign_related_fields
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/db/models/fields/related.py", line 632, in related_fields
    return self.resolve_related_fields()
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/db/models/fields/related.py", line 936, in resolve_related_fields
    related_fields = super().resolve_related_fields()
File "/home/oscar/.virtualenvs/project-venv/lib/python3.8/site-packages/django/db/models/fields/related.py", line 615, in resolve_related_fields
    raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model 'customer.user' cannot be resolved

Is there something I'm missing from the documentation ? Any ideas ?

CodePudding user response:

There seems to be an issue with your field name. Is AUTH_USER_MODEL = "customer.User" supposed to be AUTH_USER_MODEL = "customer.user" ?

CodePudding user response:

I finally managed to fix this issue by copying the content of the new migration operations list at the top of the operations list of the 0001_initial.py migration file provided by django-oscar.

  • Related