我的问题是这个。
例如下面的代码。控制台向我抛出一个关于找不到模型“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