Home > OS >  why unable to import a model in django (most likely due to a circular import)?
why unable to import a model in django (most likely due to a circular import)?

Time:09-22

I am working on a small application for learning purpose, and created a three models

CustomUser, UserProfile and PollQuestion.

CustomUser and UserProfile having OneToOne field.

And UserProfile and PollingQuestion having ForeignKey relation between them, i am also posting and code snippet bellow:-

CustomUser And UserProfile models:-

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.db.models.signals import post_save
from polls.models import PollQuestion


# Create your models here.
class CustomUser(AbstractUser):
    email = models.EmailField(max_length=250, null=False)
    name = models.CharField(max_length=50, null=False)
    username = models.CharField(max_length=50, null=False, unique=True)
    password = models.CharField(max_length=15, null=False)

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = []


class UserProfile(models.Model):
    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)

    def get_all_polls(self):
        all_polls = PollQuestion.objects.all().count()
        return 10

PollingQuestion:-

class PollQuestion(models.Model):
    question = models.TextField(max_length=250)
    user = models.ForeignKey(UserProfile, null=True, on_delete=models.CASCADE)
    option_one = models.CharField(max_length=250, null=False)
    option_two = models.CharField(max_length=250, null=False)
    option_three = models.CharField(max_length=250, null=False)
    option_four = models.CharField(max_length=250, null=False)
    option_one_votes = models.IntegerField(default=0)
    option_two_votes = models.IntegerField(default=0)
    option_three_votes = models.IntegerField(default=0)
    option_four_votes = models.IntegerField(default=0)

When i tried to run the command python manage.py makemigrations got following error:-

Traceback (most recent call last):
  File "C:\Users\akcai\OneDrive\Desktop\deep_stuffs\PollingApplication\manage.py", line 22, in <module>
    main()
  File "C:\Users\akcai\OneDrive\Desktop\deep_stuffs\PollingApplication\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\akcai\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_li
ne
    utility.execute()
  File "C:\Users\akcai\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    django.setup()
  File "C:\Users\akcai\AppData\Local\Programs\Python\Python39\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\akcai\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Users\akcai\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\config.py", line 301, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\akcai\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "C:\Users\akcai\OneDrive\Desktop\deep_stuffs\PollingApplication\polls\models.py", line 2, in <module>
    from user.models import UserProfile
  File "C:\Users\akcai\OneDrive\Desktop\deep_stuffs\PollingApplication\user\models.py", line 4, in <module>
    from polls.models import PollQuestion
ImportError: cannot import name 'PollQuestion' from partially initialized module 'polls.models' (most likely due to a circular import) (C:\Users\akcai\On
eDrive\Desktop\deep_stuffs\PollingApplication\polls\models.py)

CodePudding user response:

You can import the model in the method, so:

# no from polls.models import PollQuestion

class UserProfile(models.Model):
    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)

    def get_all_polls(self):
        from polls.models import PollQuestion
        all_polls = PollQuestion.objects.all().count()
        return 10

You thus should not import PollQuestion at the top of the file, but only in the get_all_polls method.

  • Related