我的Django项目使用三个应用程序chat, user, sowi
。每个应用程序的模型结构如下。我在启动服务器时遇到了一个错误,我想这是因为我有循环导入。如何解决此问题?
聊天/模型.py
from user.models import User
class Chat(models.Model):
name = models.CharField(max_length=100, default="private")
class Message(models.Model):
sender = models.ForeignKey(User, on_delete=models.CASCADE)
receiver = models.ForeignKey(Chat, on_delete=models.CASCADE)
用户/型号.py
from chat.models import Chat
from sowi.models import Sowi
class User(AbstractUser):
chats = models.ManyToManyField(Chat, blank=True)
subscriptions = models.ManyToManyField(Sowi, related_name="subscriptions", blank=True)
memberships = models.ManyToManyField(Sowi, related_name="memberships", blank=True)
blocked = models.ManyToManyField("self", related_name="blocked", blank=True)
sowi/models.py
from chat.models import Chat, Message
from user.models import User
class Sowi(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
chat = models.OneToOneField(Chat, blank=True, null=True, on_delete=models.CASCADE)
错误消息:
File "*/sowi/models.py", line 9, in <module>
from chat.models import Chat, Message
File "*/chat/models.py", line 4, in <module>
from user.models import User
File "*/user/models.py", line 5, in <module>
from chat.models import Chat
ImportError: cannot import name 'Chat' from 'chat.models'
提前感谢!
用字符串文字引用模型比用对模型类的引用引用模型要好。Django可以处理这个问题,并将自动解决这些问题:
# chat/models.py
from django.db import models
class Chat(models.Model):
name = models.CharField(max_length=100, default="private")
class Message(models.Model):
sender = models.ForeignKey('user.User', on_delete=models.CASCADE)
receiver = models.ForeignKey(Chat, on_delete=models.CASCADE)
# user/models.py
from django.db import models
class User(AbstractUser):
chats = models.ManyToManyField(Chat, blank=True)
subscriptions = models.ManyToManyField('sowi.Sowi', related_name="subscriptions", blank=True)
memberships = models.ManyToManyField('sowi.Sowi', related_name="memberships", blank=True)
blocked = models.ManyToManyField("self", related_name="blocked", blank=True)
# sowi/models.py
from django.db import models
class Sowi(models.Model):
owner = models.ForeignKey('user.User', on_delete=models.CASCADE)
chat = models.OneToOneField('chat.Chat', blank=True, null=True, on_delete=models.CASCADE)
当你参考用户模型[Django-doc]设置AUTH_USER_MODEL
设置[Django-dc]时,使用这个设置会更好,这样,如果你以后改变主意,你可以更方便地更改这个:
# chat/models.py
from django.conf import settings
from django.db import models
class Chat(models.Model):
name = models.CharField(max_length=100, default="private")
class Message(models.Model):
sender = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
receiver = models.ForeignKey(Chat, on_delete=models.CASCADE)
# user/models.py
from django.db import models
class User(AbstractUser):
chats = models.ManyToManyField(Chat, blank=True)
subscriptions = models.ManyToManyField('sowi.Sowi', related_name="subscriptions", blank=True)
memberships = models.ManyToManyField('sowi.Sowi', related_name="memberships", blank=True)
blocked = models.ManyToManyField("self", related_name="blocked", blank=True)
# sowi/models.py
from django.conf import settings
from django.db import models
class Sowi(models.Model):
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
chat = models.OneToOneField('chat.Chat'
try:
class User(AbstractUser):
chats = models.ManyToManyField('chat.Chat', blank=True)
subscriptions = models.ManyToManyField('sowi.Sowi', related_name="subscriptions", blank=True)
memberships = models.ManyToManyField('sowi.Sowi', related_name="memberships", blank=True)
blocked = models.ManyToManyField("self", related_name="blocked", blank=True)
代码的问题是,您在用户models.py
中导入Chat
和Sowi
,然后尝试在Chat
和Sowi
中导入用户models
。你看到环形进口了吗?