【问题标题】:Django Exception DoesNotExistDjango 异常不存在
【发布时间】:2014-07-16 07:16:33
【问题描述】:

我有一个模型,它同时具有 Django 的模型字段和 python 属性。例如:

Edit2:用实际模型更新(抱歉葡萄牙语名称)

#On Produto.models.py
from django.db          import models
from django.forms       import ModelForm
from openshift.models   import AbstractModel

from openshift.produto.models import app_name


class Produto(models.Model):

    class Meta:
        app_label = app_name


    descricao = models.CharField(max_length=128)    
    und_choices = (
        ('UND', 'Unidade'),
        ('M',     'Metro'),
        ('Kg',     'Quilograma'),
        ('PC',     'Peça'),
    )
    unidade   = models.CharField(max_length=3, choices=und_choices, default='UND')

#On Estoque.models.py

from django.db          import models
from django.forms       import ModelForm
from openshift.models   import AbstractModel
from produto.models     import Produto

from openshift.estoque.models import app_name

class Estoque(models.Model):

    class Meta:
        app_label = app_name

    id = models.IntegerField(primary_key=True)
    produtoid   = models.ForeignKey(Produto, unique=True)
    quantidade  = models.DecimalField(max_digits=20, decimal_places=4)
    valorunit   = models.DecimalField(max_digits=20, decimal_places=4)
    valortotal  = models.DecimalField(max_digits=20, decimal_places=2)   


    _field_labels = {
        "id" : r"Código", 
        "produtoid"  : r"Produto", 
        "quantidade" : r"Quantidade",
        "valorunit" : r"Valor Unitário",
        "valortotal" : r"Valor Total"
    }

    _view_order = ['id', 'produtoid', 'quantidade', 'valorunit', 'valortotal']

    @property
    def Vars(self):
        return {'field_labels': self._field_labels, 'view_order': self._view_order}

#on project.views.main_request

obj = get_model('Estoque', 'Estoque')().Vars #Here is where the Exception triggers.

如果我在调用 save() 方法之前(在创建模型记录期间)尝试调用属性“Vars”,即使“Vars”属性不是 django 的一部分,django 也会不断引发 DoesNotExist 异常型号。

谁能解释为什么会这样?

编辑:根据要求:

Django 跟踪:

追溯:

文件 “/home/gleal/cbengine/local/lib/python2.7/site-packages/django/core/handlers/base.py” 在 get_response 111. 响应 = 回调(请求,*callback_args,**callback_kwargs)

请求中的文件“/home/gleal/cbengine/engine/wsgi/openshift/views.py” 48. return browse(request, app, model, var_dict, False)

文件 “/home/gleal/cbengine/engine/wsgi/openshift/../openshift/subviews/browse.py” 在 _browse 中 32. custom_vars['TableColspan'] = len(obj.Vars.get('VIEW_ORDER', {}))

文件 “/home/gleal/cbengine/engine/wsgi/openshift/../openshift/models.py”在 变量 40. curr_vals[field.name] = getattr(self, field.name) 文件 “/home/gleal/cbengine/local/lib/python2.7/site-packages/django/db/models/fields/related.py” 在得到 343. 提高 self.field.rel.to.DoesNotExist

异常类型:DoesNotExist at /estoque/estoque/browse/ 异常 价值:

【问题讨论】:

  • 你为什么还有这个属性?
  • 你确定 my_vars 没有抛出 NameError,而不是 DoesNotExist?你能展示你用来生成错误和输出的代码吗?
  • @BurhanKhalid 它是一个“帮助”属性,它暴露了与 django 模型不直接相关的其他东西,或者补充了 django 模型属性的东西。
  • 你能发布你的财产的完整代码和完整的追溯吗?
  • @SindriGuðmundsson 是的,我很确定。代码就是“get_model(my_app,my_model)().my_vars”

标签: python django exception properties model


【解决方案1】:

刚刚发现大反派是方法Vars 上的@property 装饰器。

它使 django 尝试从类的实例中获取值,并且在某些情况下,它可能会触发 Django 的 ORM 对数据库进行的一些查询(基本上,在我试图从当前获取值的情况下)实例)。

我将@property 更改为@classmethod,一切都很顺利。像这样:

#before
@property
def Vars(self):
    return {'field_labels': self._field_labels, 'view_order': self._view_order}

#after
@classmethod
def Vars(self):
    return {'field_labels': self._field_labels, 'view_order': self._view_order}

感谢任何试图提供帮助的人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-12
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 2020-06-01
    • 2017-11-07
    • 2016-07-12
    相关资源
    最近更新 更多