1

我的问题是这个。

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

2 回答 2

0

解决它的一种方法是在同一个文件中定义modelbbefore 。modelaPython是一种脚本语言,是逐行执行的,所以当你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自动添加并允许获取通过外键链接到对象的所有条目的列表。

于 2015-06-08T16:54:52.053 回答
-1

根据您上次的评论,

“我再次编辑。但现在错误在于尝试制作precio = MLPreciosProductos.objects.filter(plantilla=self)[0]或找到一个”

我认为您无法过滤,只需self尝试更改它self.idself.nombre您想要的任何内容,只要它在类上定义即可。原谅我的英语不好,只是想帮忙

于 2012-11-12T11:47:45.827 回答