一尘不染

Django无法导入名称x

django

我有一个我不明白的错误!

无法导入名称项目

在我的模型中,我有项目。这些项是操作所必需的。但是其中一些项目会影响操作:

项目

from django.db import models
from effects.models import Effect

class Type(models.Model):
    name = models.CharField(max_length=200)

    def __unicode__(self):
         return self.name

class Item(models.Model):
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=200)
    type = models.ForeignKey(Type)
    quality = models.IntegerField()
    effects = models.ManyToManyField(Effect,through='ItemEffect',blank=True)
    item_requirement = models.ManyToManyField('self',through='ItemCraft',symmetrical=False,blank=True)
points = models.IntegerField()

    def __unicode__(self):
        return self.name

class Food(Item):
    ap = models.IntegerField()

class Tool(Item):
    durability = models.IntegerField()

[....]

class ItemEffect(models.Model):
    item = models.ForeignKey(Item)
    effect = models.ForeignKey(Effect)

def __unicode__(self):
    return self.item.name+':'+str.lower(self.effect.name)

class Meta:
    verbose_name_plural = 'items effects'

class ItemCraft(models.Model):
    item = models.ForeignKey(Item,related_name='%(class)s_item_crafted')
    item_requirement = models.ForeignKey(Item,related_name='%(class)s_item_required')
    number = models.IntegerField()

    def __unicode__(self):
        return self.item.name+' requires '+str.lower(self.item.name)+'('+self.number+')'

    class Meta:
        verbose_name_plural = 'items crafts'

行动

from django.db import models
from items.models import Item

class Action(models.Model):
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=200)
    pa = models.IntegerField()

    def __unicode__(self):
        return self.name

class CraftAction(Action):
    item = models.ForeignKey(Item)

    def __unicode__(self):
        return self.item.name+'\'s craft'

    class Meta:
        verbose_name_plural = 'crafts actions'

效果

from django.db import models
from actions.models import Action

class Effect(models.Model):
    action = models.ForeignKey

class ApEffect(Effect):
    ap = models.IntegerField()

阅读 280

收藏
2020-04-01

共1个答案

一尘不染

你的代码中有循环导入,这就是为什么无法实际导入Item的原因。

你可以通过删除其中一个文件中的类导入并将其替换为包含该类名称的字符串来解决该问题。例如 :

effects = models.ManyToManyField('effects.Effect',through='ItemEffect',blank=True)
2020-04-01