Django 导入自定义用户模型

Django importing a custom user model

我有一个带有自定义用户模型的 Django 应用程序。我正在尝试将此模型导入另一个应用程序。到目前为止我不知道该怎么做。

accounts/models.py 处的客户用户模型是

from django.db import models
from machina.core.db.models import get_model
from django.db.models import Q
from django.contrib.auth.models import Group
# signals imports
from django.dispatch import receiver
from django.db.models.signals import (
    post_save
)

Forum = get_model("forum", "Forum")


class CustomUser(AbstractUser):
    first_name = models.CharField(max_length=250, null=True)
    last_name = models.CharField(max_length=250, null=True)
    age = models.PositiveIntegerField(null=True, blank=True)
    business_name = models.CharField(max_length=250, null=True)
    business_location_state = models.ForeignKey(
        Forum, null=True,  on_delete=models.SET_NULL, limit_choices_to={"lft": 1})
    business_location_county = models.ForeignKey(
        Forum, null=True, on_delete=models.SET_NULL, related_name='county')
    business_location_city = models.CharField(max_length=1024, null=True)
    business_zipcode = models.CharField(max_length=12, null=True)

我最近尝试导入此模型的代码是:

from django.db import models
from django.contrib.auth.models import User
from django_tenants.models import DomainMixin, TenantMixin
from django.utils.translation import ugettext as _
from localflavor.us.models import USStateField
from accounts.models import CustomUser


class Tenant(TenantMixin):
    user = models.ForeignKey('CustomUser', on_delete=models.SET_NULL, null=True)
    company_name = models.CharField(max_length=100)
    address_1 = models.CharField(_("address"), max_length=128)
    address_2 = models.CharField(_("address cont'd"), max_length=128, blank=True)
    city = models.CharField(_("city"), max_length=64)
    state = USStateField(_("state"))
    zip_code = models.CharField(_("zip code"), max_length=5)
    created_on = models.DateField(auto_now_add=True)

当我尝试启动服务器时出现此错误:

django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
company_accounts.Tenant.user: (fields.E300) Field defines a relation with model 'CustomUser', which is either not installed, or is abstract.
company_accounts.Tenant.user: (fields.E307) The field company_accounts.Tenant.user was declared with a lazy reference to 'company_accounts.customuser', but app 'company_accounts' doesn't provide model 'customuser'.

您在引号之间添加 Customuser 模型.. 只需删除这样的引号:

user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)

再试一次