我正在尝试创建一个系统,以便某个用户对其他用户发表评论。因此,我创建了一个GenericRelation像这样的模型:App1 \ models.py:from django.contrib.auth.models import AbstractUserfrom django.contrib.contenttypes.fields import GenericRelationfrom app2.models import Socialclass User(AbstractUser): """ Default User model used for authentification system """ relation = GenericRelation(Social) # recently addedApp2 \ models.py:from django.contrib.contenttypes.fields import GenericForeignKeyfrom django.contrib.contenttypes.models import ContentTypefrom app1.models import Userclass Social(models.Model): content_object = GenericForeignKey('content_type', 'object_id') object_id = models.PositiveIntegerField() content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) author= models.ForeignKey(User, # Member who wrote the message on_delete=models.CASCADE, related_name='wrote') message= models.TextField()之前添加场relation在User。我没有任何问题,但我正在重做工作,GenericRelation我想简化我的鳕鱼。然后当我运行服务器时出现问题。我处于递归导入循环中...file .\app2\models.py from app2.models import Socialfile .\app1\models.py from app1.models import User是否有可能通过对“我的GenericRelation社交”和GenericRelation“我的社交媒体”保持关注来解决此问题User?因为之后我想GenericRelation(Social)在其他模型上添加另一个
1 回答
烙印99
TA贡献1829条经验 获得超13个赞
尝试将User模型引用为中的字符串ForeignKey,如下所示:
author = models.ForeignKey(
'app1.User',
on_delete=models.CASCADE,
related_name='wrote')
)
并且,也删除from app1.models import User。
添加回答
举报
0/150
提交
取消