I have two apps(front and lrnadmin) in project(portal) this is my root directory
the problem is that when I import the models in each other it gives me error
In this models I want Qualification as Foreign key that why I want import Qualification from lrnadmin.models
this is my front.models.py
from datetime import datetime, timezone
from django.db import models
from django.db.models import expressions
from django.db.models.base import Model
from django.utils import timezone
from lrnadmin.models import Qualification
class Staff(models.Model):
"""Model definition for Staff."""
# TODO: Define fields here
nStaff = models.CharField(max_length= 50)
role = models.CharField(max_length= 50)
lrnQ = models.ForeignKey(Qualification, on_delete= models.CASCADE)
relQ = models.CharField(max_length=200)
relE = models.CharField(max_length=200)
# Cv = models.CharField(max_length=100, blank=True, null=True)
# Doc = models.CharField(max_length=100, blank=True, null=True)
sec7 = models.ForeignKey(Sec7, on_delete=models.CASCADE)
class Meta:
"""Meta definition for Staff."""
verbose_name = 'Staff'
verbose_name_plural = 'Staffs'
# def __str__(self):
# """Unicode representation of Staff."""
# pass
and in this models I want User as Foreign key that why I want import User from front.models
this is my lrnadmin.models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils import timezone
from front.models import User
class CentreCode(models.Model):
"""Model definition for CentreCode."""
# TODO: Define fields here
user = models.ForeignKey(User, on_delete= models.CASCADE)
centreCode = models.CharField(max_length= 20)
categories = models.CharField(max_length = 20)
authorisation = models.CharField(max_length= 20)
registrationType = models.CharField(max_length= 20)
class Meta:
"""Meta definition for CentreCode."""
verbose_name = 'CentreCode'
verbose_name_plural = 'CentreCodes'
# def __str__(self):
# """Unicode representation of CentreCode."""
# pass
How Can I handle this
File "E:\lrn\portal\lrnadmin\models.py", line 4, in <module>
from front.models import User
File "E:\lrn\portal\front\models.py", line 7, in <module>
from lrnadmin.models import Qualification
ImportError: cannot import name 'Qualification' from partially initialized module 'lrnadmin.models' (most likely due to a circular import) (E:\lrn\portal\lrnadmin\models.py)
CodePudding user response:
you can try out the get_model which will avoid importing the model at run time
from django.apps import apps
qualification_model = app.get_model('lrnadmin', 'Qualification')
OR
you can try to import the model inside the function instead of importing at module level
CodePudding user response:
You can "lazy-load" a model by using a string literal as described in the docs
, so for example with the front
app's User
model you can define it like:
# Remove from front.models import User
class CentreCode(models.Model):
# ...
user = models.ForeignKey('front.User', on_delete= models.CASCADE)
# ...
Or if you defined front.models.User
as a custom user model, it's highly suggested that you just reference the custom model using settings.AUTH_USER_MODEL
as described here
:
from django.conf import settings
# Remove from front.models import User
class CentreCode(models.Model):
# ...
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete= models.CASCADE)
# ...