app1:title // title.models.py
from django.db import models
from relationship.models import ContentRelationship
from django.contrib.auth.models import User
class Title(models.Model):
titleText = models.TextField(max_length=160, unique=True)
titleRelationship = models.OneToOneField('relationship.ContentRelationship', on_delete=models.CASCADE)
def __str__(self):
return self.titleText
app2:关系// relationship.models.py
from title.models import Title
class ContentRelationship(models.Model):
allow_set = models.ManyToManyField('UserRelationship', related_name="wdt_set", symmetrical=False, through="Wdt", blank=True)
description = models.TextField(max_length=200, blank=True)
def __str__(self):
return self.description
@receiver(post_save, sender=Title)
def create_contentrelationship(sender, instance, created, **kwargs):
if created:
ContentRelationship.objects.create(Title=instance)
@receiver(post_save, sender=Title)
def save_contentrelationship(sender, instance, **kwargs):
instance.contentrelationship.save()
我想要的是,如果我创建了Title实例,那么也会创建ContentRelationship。
但是在那段代码上,有
File "C:/Dev/TestProject/Test1\title\models.py", line 2, in <module>
from relationship.models import ContentRelationship
ImportError: cannot import name 'ContentRelationship'
我搜索并谨慎地ForeignKey or ManyToManyField
来论证'relationship.ContentRelationship'
。换句话说,我把它变成了字符串。
但它不起作用。
修改 我的项目结构:
我删除的部分名字很差。所以我删除了它。遗憾。
答案 0 :(得分:3)
尝试删除此导入:
from relationship.models import ContentRelationship
因为你不需要它。您正在使用字符串
'relationship.ContentRelationship'