gpt4 book ai didi

python - Django 无法导入名称 x

转载 作者:IT老高 更新时间:2023-10-28 20:34:44 26 4
gpt4 key购买 nike

我遇到了一个我不明白的错误!

无法导入名称项目

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

项目

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()

最佳答案

您的代码中存在循环导入,这就是无法实际导入项目的原因。

您可以通过删除其中一个文件中的类的导入并将其替换为包含类名称的字符串 as explained in the documentation 来解决此问题。 .例如:

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

关于python - Django 无法导入名称 x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7684408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com