ami*_*_70 1 django django-models django-migrations
我有两个应用程序“用户”和“游戏”,这是我的 user/models.py:
from django.db import models
from django.db.models.fields import related
from django.contrib.auth.models import AbstractBaseUser , User,
AbstractUser , PermissionsMixin
from .managers import CustomUserManager
from game.models import Game
class User(AbstractBaseUser,PermissionsMixin):
username = models.CharField(max_length=150,
unique=True, blank=True , null=True,)
email = models.EmailField( blank=True , null=True , unique=True)
password = models.CharField(max_length=100, null=True , blank=True)
objects = CustomUserManager()
class Meta:
db_table = 'user'
class Team(models.Model):
name = models.CharField(max_length=40, unique=True)
players = models.ManyToManyField(User,
related_name="players")
game = models.ManyToManyField(Game)
is_verfied = models.BooleanField(default=False)
class Meta:
db_table = 'team'
Run Code Online (Sandbox Code Playgroud)
这是我的游戏/models.py:
from django.db import models
from user.models import User, Team
from leaga.settings import AUTH_USER_MODEL
class Game(models.Model):
name = models.CharField(max_length=50)
is_multiplayer = models.BooleanField(default=False)
class Event(models.Model):
title = models.CharField(max_length=100)
start_date = models.DateTimeField()
end_date = models.DateTimeField()
class Tournament(models.Model):
title = models.CharField(max_length=50)
is_team = models.BooleanField(default=False)
price = models.PositiveIntegerField()
info = models.TextField(max_length=1000)
user = models.ManyToManyField(AUTH_USER_MODEL,
related_name='tounament_user')
team = models.ManyToManyField(Team, related_name='team')
game = models.ForeignKey(Game, on_delete=models.CASCADE, related_name='tournament_game')
event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name='tournament_event')
# some other code and classes here but unnecessary to mention
Run Code Online (Sandbox Code Playgroud)
。。。。当我跑步时:
python manage.py makemigrations user
Run Code Online (Sandbox Code Playgroud)
这是控制台日志:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Users/amir/Desktop/leaga/.venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Users/amir/Desktop/leaga/.venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/Users/amir/Desktop/leaga/.venv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/amir/Desktop/leaga/.venv/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/Users/amir/Desktop/leaga/.venv/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/Users/amir/Desktop/leaga/.venv/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/amir/Desktop/leaga/leaga/user/models.py", line 6, in <module>
from game.models import Game
File "/Users/amir/Desktop/leaga/leaga/game/models.py", line 3, in <module>
from user.models import Team
ImportError: cannot import name 'Team' from 'user.models' (/Users/amir/Desktop/leaga/leaga/user/models.py)
Run Code Online (Sandbox Code Playgroud)
我尝试从项目中删除所有 .pyc 文件
另外,删除整个数据库并再次创建(我知道这可能不是重点,但我很生气,我删除了整个数据库并重新创建它
文件“/Users/amir/Desktop/leaga/leaga/user/models.py”,第 6 行,位于
从 game.models 导入游戏
文件“/Users/amir/Desktop/leaga/leaga/game/models.py”,第 3 行,位于
从 user.models 导入团队
ImportError:无法从“user.models”导入名称“Team”(/Users/amir/Desktop/leaga/leaga/user/models.py)
从消息中我们可以看出这是由于循环导入造成的。
user/models.pyGame尝试从导入模型game/models.py,并尝试从game/models.py导入模型。Teamuser/models.py
一种解决方案是删除:
from game.models import Game
Run Code Online (Sandbox Code Playgroud)
从user/models.py, 并改变:
game = models.ManyToManyField(Game)
Run Code Online (Sandbox Code Playgroud)
到:
game = models.ManyToManyField('game.Game')
Run Code Online (Sandbox Code Playgroud)
您可能想阅读:ForeignKey和ManyToManyField。
相关部分复制到这里:
如果需要在尚未定义的模型上创建关系,可以使用模型的名称,而不是模型对象本身:
| 归档时间: |
|
| 查看次数: |
4570 次 |
| 最近记录: |