我正在開發一個用于學習的小型應用程式,并創建了三個模型
CustomUser、UserProfile和PollQuestion。
CustomUser, UserProfile 和 PollQuestion。 CustomUser和UserProfile有OneToOne欄位。
并且UserProfile和PollingQuestion之間有外鍵關系。
我也張貼和代碼片段如下:- CustomUser 和 UserProfile 模型:- PollingQuestion:- 當我試圖運行命令 uj5u.com熱心網友回復: 你可以在方法中匯入模型,因此: 因此,你不應該在檔案的頂部匯入
標籤: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
#在這里創建你的模型。
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 = '用戶名'
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
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(默認=0)
option_three_votes = models.IntegerField(默認=0)
option_four_votes = models.IntegerField(default=0)
python manage.py makemigrations時,得到了以下錯誤:-Traceback (most recent call last):
檔案 "C:UsersakcaiOneDriveDesktopdeep_stuffsPollingApplicationmanage.py", 行 22, in < module>
主體()
檔案 "C:UsersakcaiOneDriveDesktopdeep_stuffsPollingApplicationmanage.py", 行 18, in main
execute_from_command_line(sys.argv)
檔案 "C:UsersakcaiAppDataLocalProgramsPython39libsite-packagesdjangocoremanagement\__init__.py", 行 419, in execute_from_command_li
氖
utility.execute()
檔案 "C:UsersakcaiAppDataLocalProgramsPython39libsite-packagesdjangocoremanagement\__init__.py", 行 395, in execute
django.setup()
檔案 "C:UsersakcaiAppDataLocalProgramsPython39libsite-packagesdjango\__init__.py", 行 24, in setup
apps.populate(settings.INSTALLED_APPS)
檔案"C:UsersakcaiAppDataLocalProgramsPythonPython39libsite-packagesdjangoapps
egistry.py",第114,in populate
app_config.import_models()
檔案 "C:UsersakcaiAppDataLocalProgramsPython39libsite-packagesdjangoappsconfig.py", 行 301, in import_models
self.models_module = import_module(models_module_name)
檔案 "C:UsersakcaiAppDataLocalProgramsPython39libimportlib\__init__.py", 行 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
檔案 "<rozen importlib._bootstrap>", 行 1030, in _gcd_import
檔案 "<frozen importlib._bootstrap>", line 1007, in _find_and_load
檔案 "<rozen importlib._bootstrap>", 行 986, in _find_and_load_unlocked
檔案 "<rozen importlib._bootstrap>", 行 680, in _load_unlocked
檔案 "<rozen importlib._bootstrap_external>", 行 850, in exec_module
檔案 "<rozen importlib._bootstrap>", line 228, in _call_with_frames_removed
檔案 "C:UsersakcaiOneDriveDesktopdeep_stuffsPollingApplicationpollsmodels.py", 行 2, in <module>
from user.models import UserProfile
檔案 "C:UsersakcaiOneDriveDesktopdeep_stuffsPollingApplicationusermodels.py", 行 4, in<module>
from polls.models import PollQuestion
ImportError: cannot import name 'PollQuestion' from partially initialized module 'polls'. models'(很可能是由于回圈import)(C:UsersakcaiOn
eDriveDesktopdeep_stuffsPollingApplicationpollsmodels.py)
。
# 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 10PollQuestion,而應該只在get_all_polls方法中匯入。
