我的问题是这个。
例如以下代码。控制台抛出一个关于没有找到模型'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)
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时,抛出异常
Traceback:
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/opt/marketland.es/web/marketland/views.py" in outlet_campana
127. context_instance=RequestContext(request))
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render_to_response
20. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
188. return t.render(context_instance)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/base.py" in render
123. return self._render(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
127. return compiled_parent._render(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
127. return compiled_parent._render(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
64. result = block.nodelist.render(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
64. result = block.nodelist.render(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
227. nodelist.append(node.render(context))
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/debug.py" in render
90. output = self.filter_expression.resolve(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/base.py" in resolve
510. obj = self.var.resolve(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/base.py" in resolve
653. value = self._resolve_lookup(context)
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/template/base.py" in _resolve_lookup
698. current = current()
File "/home/opt/marketland.es/web/marketland/models.py" in get_precio
201. precio = MLPreciosProductos.objects.filter(plantilla=self)[0]
File "/home/opt/ENV-django/local/lib/python2.7/site-packages/django/db/models/query.py" in __getitem__
190. return list(qs)[0]
Exception Type: TemplateSyntaxError at /outlet/atornillador-blackampdecker-pilas-aaa-558/
Exception Value: Caught IndexError while rendering: list index out of range
答案 0 :(得分:0)
解决此问题的一种方法是在同一文件中modelb
之前定义modela
。
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)_set
会自动添加,并允许通过外键获取链接到该对象的所有条目的列表。
答案 1 :(得分:-1)
根据您上次的评论,
“我再次进行了编辑。但由于尝试制作precio = MLPreciosProductos.objects.filter(plantilla=self)[0]
或找到一个”
我认为您无法使用self
进行过滤,只要在课程中定义,就可以尝试使用self.id
或self.nombre
进行更改。请原谅我的英语不好,只是想帮忙