我有一个模型,它同时具有Django的模型字段和python属性。例如:
编辑2:更新了实际模型(对不起葡萄牙语名称)
#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.
如果我试图致电该物业" Vars"在我调用save()方法之前(在创建模型记录期间),django不断提出一个DoesNotExist异常,即使" Vars"属性不是django模型的一部分。
任何人都可以解释为什么会这样吗?
编辑:按要求:
Django Trace:
回溯:
文件 " /home/gleal/cbengine/local/lib/python2.7/site-packages/django/core/handlers/base.py" 在get_response中 111. response = callback(request,* callback_args,** callback_kwargs)
File" /home/gleal/cbengine/engine/wsgi/openshift/views.py"在req 48.返回浏览(request,app,model,var_dict,False)
文件 " /家庭/ gleal / cbengine /发动机/ WSGI / openshift /../ openshift /子视图/ browse.py" 在_browse 32. custom_vars [' TableColspan'] = len(obj.Vars.get(' VIEW_ORDER',{}))
文件 " /家庭/ gleal / cbengine /发动机/ 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
异常类型:在/ estoque / estoque / browse / Exception中的DoesNotExist 值:
答案 0 :(得分:0)
刚刚发现好恶棍是方法@property
上的Vars
装饰者。
正在让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}
感谢任何试图提供帮助的人!