从 Django-Oscar AbstractUser 模型扩展的问题
Troubles extending from Django-Oscar AbstractUser model
我一直在尝试 fork django-oscar customer
应用程序。我遵循了他们文档中的指南,但由于 ValueError: Related model 'customer.user' cannot be resolved
,我无法应用迁移。
我的项目目录如下所示:
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
以下是我达到现在的状态所遵循的步骤:
./manage.py oscar_fork_app customer .
- 我将
customer
应用程序添加到我的 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
如您所见,我尝试了“oscar”添加应用程序的方式,方法是指定他们使用命令生成的 CustomerConfig
方法的路径,但没有帮助,所以我尝试了常规的“django” " 方式,但它也不起作用。
- 然后我将新模型添加到
AUTH_USER_MODEL
设置键,如下所示:
AUTH_USER_MODEL = "customer.User"
当我尝试进行迁移时,它按预期工作,并在上面的列表中创建了迁移编号 9,但是当我尝试应用迁移时,出现此错误:
./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
文档中是否缺少某些内容?有什么想法吗?
您的字段名称似乎有问题。 AUTH_USER_MODEL = "customer.User"
应该是 AUTH_USER_MODEL = "customer.user"
吗?
我终于设法通过复制 django 提供的 0001_initial.py
迁移文件的 operations
列表顶部的新迁移 operations
列表的内容来解决这个问题-奥斯卡.
我一直在尝试 fork django-oscar customer
应用程序。我遵循了他们文档中的指南,但由于 ValueError: Related model 'customer.user' cannot be resolved
,我无法应用迁移。
我的项目目录如下所示:
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
以下是我达到现在的状态所遵循的步骤:
./manage.py oscar_fork_app customer .
- 我将
customer
应用程序添加到我的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
如您所见,我尝试了“oscar”添加应用程序的方式,方法是指定他们使用命令生成的 CustomerConfig
方法的路径,但没有帮助,所以我尝试了常规的“django” " 方式,但它也不起作用。
- 然后我将新模型添加到
AUTH_USER_MODEL
设置键,如下所示:
AUTH_USER_MODEL = "customer.User"
当我尝试进行迁移时,它按预期工作,并在上面的列表中创建了迁移编号 9,但是当我尝试应用迁移时,出现此错误:
./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
文档中是否缺少某些内容?有什么想法吗?
您的字段名称似乎有问题。 AUTH_USER_MODEL = "customer.User"
应该是 AUTH_USER_MODEL = "customer.user"
吗?
我终于设法通过复制 django 提供的 0001_initial.py
迁移文件的 operations
列表顶部的新迁移 operations
列表的内容来解决这个问题-奥斯卡.