Django无法导入名称x [英] Django cannot import name x

查看:88
本文介绍了Django无法导入名称x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误我不明白



无法导入名称项


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



项目

  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',symmetry = False,blank = True)
points = models .IntegerField()

def __unicode __(self):
return self.name

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

class工具(Item):
durable = models.Integ erField()

[....]

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 ='% )
item_requirement = models.ForeignKey(Item,related_name ='%(class)s_item_required')
number = models.IntegerField()

def __unicode __(self) :
return self.item.name +'require'+ 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 __自我):
return self.item.name +'\'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()


解决方案

您的代码中有一个循环导入,这就是为什么无法导入项目。



您可以通过删除来解决问题进口您的其中一个文件中的类,并将其替换为包含该类名称的字符串,如文档中所述。例如:

  effects = models.ManyToManyField('effects.Effect',through ='ItemEffect',blank = True)


I got an error I don't understand !

cannot import name Item

In my model, I have items. These items are required for actions. But some of these items have an effect on actions :

items

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'

actions

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'

effects

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

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

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

解决方案

There is a circular import in your code, that's why the Item can't be imported in action.

You can solve the problem by removing the import of a class in one of your files, and replacing it with a string containing the name of the class, as explained in the documentation. For example :

effects = models.ManyToManyField('effects.Effect',through='ItemEffect',blank=True)

这篇关于Django无法导入名称x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆