Django Models - Circular-import-issue
My Django-Project uses Three Apps, chat, user, sowi. Every App having models structured in the following way. I'm getting an error when starting the Server, i think it is because i
Solution 1:
It is better to refer to models with string literals, than to refer to it with references to the model class. Django can handle this and will automatically resolve these:
# chat/models.pyfrom django.db import models
classChat(models.Model):
name = models.CharField(max_length=100, default="private")
classMessage(models.Model):
sender = models.ForeignKey('user.User', on_delete=models.CASCADE)
receiver = models.ForeignKey(Chat, on_delete=models.CASCADE)
# user/models.pyfrom django.db import models
classUser(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.pyfrom django.db import models
classSowi(models.Model):
owner = models.ForeignKey('user.User', on_delete=models.CASCADE)
chat = models.OneToOneField('chat.Chat', blank=True, null=True, on_delete=models.CASCADE)
It is even better when you reference the user model [Django-doc] to set the AUTH_USER_MODEL
setting [Django-doc], and use this setting, such that if you later change your mind, you can more conveniently change this:
# chat/models.pyfrom django.conf import settings
from django.db import models
classChat(models.Model):
name = models.CharField(max_length=100, default="private")
classMessage(models.Model):
sender = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
receiver = models.ForeignKey(Chat, on_delete=models.CASCADE)
# user/models.pyfrom django.db import models
classUser(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.pyfrom django.conf import settings
from django.db import models
classSowi(models.Model):
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
chat = models.OneToOneField('chat.Chat'
Solution 2:
try:
classUser(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)
The problem with your code is that you're importing Chat
and Sowi
in user models.py
, and then you're trying to import user models
in Chat
and Sowi
. You see the Circular Import there?
Post a Comment for "Django Models - Circular-import-issue"