Home > database >  Django importing a custom user model
Django importing a custom user model

Time:11-21

I have a Django app with a custom user model. I am attempting to import this model into another app. So far I can't figure out how to do this.

The customer user model at accounts/models.py is

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)

The code I most recently tried to import this model is:

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)

When I attempt to start the server I get this error:

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'.

CodePudding user response:

you add Customuser model between quotes .. just remove quotes like this :

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

and try again

  • Related