Django模型函数中的错误
我的问题是这个 例如,下面的代码。控制台向我抛出一个关于找不到模型“ModelB”的错误Django模型函数中的错误,django,django-models,Django,Django Models,我的问题是这个 例如,下面的代码。控制台向我抛出一个关于找不到模型“ModelB”的错误 class modela(models.Model): id = models.IntegerField(primary_key=True) name = models.CharField(max_length=360) def print_name_modelb(self): modelb = modelb.objects.get(id=1) pr
class modela(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=360)
def print_name_modelb(self):
modelb = modelb.objects.get(id=1)
print modelb.name
class modelb(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=360)
如果识别失败,是否有办法设置所有空模型以用于功能
用真实数据编辑
class Vip2ProductosPlantilla(models.Model):
id = models.IntegerField(primary_key=True)
nombre = models.CharField(max_length=360)
referencia = models.CharField(max_length=300)
intro = models.TextField()
descripcion = models.TextField()
precio_tienda = models.DecimalField(max_digits=12, decimal_places=2)
precio_vip = models.DecimalField(max_digits=12, decimal_places=2)
precio_cesion = models.DecimalField(max_digits=12, decimal_places=2)
zoom = models.BooleanField()
id_marca = models.ForeignKey(Vip2Marcas, null=True, db_column='id_marca', blank=True)
categoria = models.ManyToManyField(Vip2Categorias)
stock = models.IntegerField(null=True, blank=True, default=0)
last_update = models.DateTimeField(auto_now=True)
gastos_envio = models.DecimalField(max_digits=12, decimal_places=2,blank=True,null=True)
def get_precio(self):
try:
precio = MLPreciosProductos.objects.filter(plantilla=self)[0]
if (precio != None) & (precio.precio_ml > 0):
print 'precio ml'
return precio.precio_ml
else:
print 'precio vip'
return self.precio_vip
except:
print 'precio vip error'
return self.precio_vip
class MLPreciosProductos(models.Model):
id = models.AutoField(primary_key=True)
plantilla = models.ForeignKey(Vip2ProductosPlantilla,null=True, db_column='plantilla', blank=True)
precio_ml = models.DecimalField(max_digits=12, decimal_places=2)
campana = models.ForeignKey(Vip2Campanas, null=True, db_column='campana', blank=True)
当模板函数调用get_precio时,将引发异常
根据你上次的评论 “我再次编辑了。但是现在出现了错误,因为尝试进行
precio=MLPreciosProductos.objects.filter(plantilla=self)[0]
或查找一个”
我认为您不能仅使用
self
进行筛选,请尝试使用self.id
或self.nombre
更改它,只要它在类中定义即可。请原谅我英语不好,我只是想帮你解决这个问题的一个方法是在同一个文件中定义modela
之前定义modelb
。
Python是一种脚本语言,是逐行执行的,因此当您在示例中提到modelb
时,它还不存在。但是,有时您需要两个模型在
ForeignKey
中相互引用,在这种情况下,您可以将模型名称作为字符串传递:
campana = models.ForeignKey('Vip2Campanas')
另一个可能导致问题的因素是过滤中的对象:
MLPreciosProductos.objects.filter(plantilla=self)
由于对象可以动态更改,并且实际上是指针,因此最好通过其id来标识对象,如下所示:
MLPreciosProductos.objects.filter(plantilla_id=self.id)
或者使用django在幕后创建的反向查找:
Foo(models.Model):
pass
Bar(models.Model):
foo = models.ForeignKey('Foo')
# instance of a Foo model is given 'hidden' field bar_set
foo.bar_set
(modelname)\u set
是自动添加的,允许获取通过外键链接到对象的所有条目的列表。它们都在同一个文件中,正如您在这里显示的那样吗?您是否执行了:'python manage.py syncdb'是的,我将所有模型都放在同一个文件中确定,您可以用回溯显示准确的错误消息,请澄清你的问题。如果你得到一个例外,请张贴。到目前为止,你没有提供足够的信息让任何人回答你的问题。谢谢你的回答。如果我能自己过滤。现在一切正常,我不知道为什么。刚刚添加到我的try/except此行以显示控制台exepcion打印“\n”.join(traceback.format\u exception(*sys.exc\u info())
Foo(models.Model):
pass
Bar(models.Model):
foo = models.ForeignKey('Foo')
# instance of a Foo model is given 'hidden' field bar_set
foo.bar_set