我已经将User模型从Blogs应用程序中的accounts应用程序导入,并将BlogPost模型导入User应用程序。但我得到了这个循环的imort问题。如何解决这个问题??
Accounts.models.py看起来像这样:
from apps.blogs.models import BlogPost
class Subscribers(models.Model):
email = models.EmailField(unique=True)
date_subscribed = models.DateField(auto_now_add=True)
def __str__(self):
return self.email
class Meta:
verbose_name_plural = "Newsletter Subscribers"
# binding signal:
@receiver(post_save,sender=BlogPost)
def send_mails(sender,instance,created,**kwargs):
subscribers = Subscribers.objects.all()
if created:
for abc in subscribers:
emailad = abc.email
send_mail('New Blog Post ', f" Checkout our new blog with title {instance.title} ",
emailad, [emailad],
fail_silently=False)
else:
return
我的博客模型.py
from apps.accounts.models import User
class BlogPost(models.Model):
author = models.CharField(max_length=64, default='Admin')
image = models.ImageField(blank=True, null=True)
title = models.CharField(max_length=255)
.............
class Comment(models.Model):
# blog = models.ForeignKey(BlogPost, on_delete=models.CASCADE, default=1)
user = models.ForeignKey(User, on_delete=models.CASCADE)
blog = models.ForeignKey(BlogPost, on_delete=models.CASCADE, related_name='comments')
name = models.CharField(max_length=255)
email = models.EmailField()
.................
我也试过使用
from django.contrib.auth import get_user_model
User = get_user_model()
但这给带来了另一个问题
django.core.exceptions.ImproverlyConfigured:AUTH_USER_MODEL指的是尚未安装的模型"accounts.USER">
当我在accounts.models.py中导入BlogPost模型时,没有出现任何异常。所以,我陷入了困境。
不要将User
模型导入博客models.py
,而是这样更改它。
user = models.ForeignKey("accounts.User", on_delete=models.CASCADE)
blog = models.ForeignKey("blogs.BlogPost", on_delete=models.CASCADE, related_name='comments')