我有两个应用程序。
relationship_api
模型有点复杂。在将ImportError: cannot import name Character
添加到manner
模型后,以下模型结构会出现character
错误。
character_api.models
from django.db import models
from django.contrib.auth.models import User
from relationship_api.models import RelationshipType
class Manner(models.Model):
name = models.CharField(max_length=100)
delayed_types = models.ManyToManyField(RelationshipType)
allowed_types = models.ManyToManyField(RelationshipType)
denied_types = models.ManyToManyField(RelationshipType)
class Character(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=50)
surname = models.CharField(max_length=50)
date_created = models.DateField()
age = models.SmallIntegerField()
health = models.SmallIntegerField()
happiness = models.SmallIntegerField()
depression = models.SmallIntegerField()
manner = models.ForeignKey(Manner)
relationship_api.models
# -*- coding: utf-8 -*-
from django.db import models
from character_api.models import Character
from django.core.exceptions import ValidationError
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class RelationshipType(models.Model):
name = models.CharField(max_length=500)
def __str__(self):
return self.name
class RelationEffect(models.Model):
type = models.ForeignKey(RelationshipType)
effect = models.SmallIntegerField()
@python_2_unicode_compatible
class RelationshipLevel(models.Model):
name = models.CharField(max_length=500)
type = models.ForeignKey(RelationshipType)
level_order = models.SmallIntegerField()
def __str__(self):
return self.name
@python_2_unicode_compatible
class Interact(models.Model):
name = models.CharField(max_length=500)
available_type = models.ForeignKey(RelationshipType)
available_level = models.ForeignKey(RelationshipLevel)
effect_on_level = models.SmallIntegerField()
other_effects = models.ManyToManyField(RelationEffect)
def __str__(self):
return self.name
@python_2_unicode_compatible
class Interaction(models.Model):
interact = models.ForeignKey(Interact)
actor = models.ForeignKey(Character)
date = models.DateTimeField()
def __str__(self):
return self.pk
class Relation(models.Model):
type = models.ForeignKey(RelationshipType)
level = models.ForeignKey(RelationshipLevel)
percentage = models.SmallIntegerField()
class Relationship(models.Model):
characters = models.ManyToManyField(Character)
relation = models.OneToOneField(Relation)
interactions = models.ManyToManyField(Interaction)
def clean(self, *args, **kwargs):
if self.characters.count() != 2:
raise ValidationError("There has to be 2 characters for relationship")
super(Relationship, self).clean(args, kwargs)
def __str__(self):
return self.pk
我认为我的模型是正确导入的,但关系模型的复杂性在某些时候会崩溃,但我无法找到它发生的位置。
答案 0 :(得分:2)
您有循环导入。
character_api.models
包含from relationship_api.models import RelationshipType
,然后relationship_api.models
包含from character_api.models import Character
答案 1 :(得分:2)
循环导入。
像这样更换你的模型:
models.ManyToManyField(RelationshipType)
models.ManyToManyField('relationship_api.RelationshipType')
和models.ForeignKey(Character)
models.ForeignKey('character_api.Character')
SecureRandom
。{。}
不要从模型导入。