Django Exception DoesNotExist
I have a model that has Django's model fields AND python properties at the same time. Ex:
Edit2: Updated with actual models (sorry for the portuguese names)
#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.
If i try to call the property "Vars" before I call the save() method (during the creation of the model's record), django keeps raising a DoesNotExist exception, even though the "Vars" property isnt part of the django model.
Can anyone explain why is this happening?
Edit: As Requested:
Django Trace:
Traceback:
File "/home/gleal/cbengine/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/gleal/cbengine/engine/wsgi/openshift/views.py" in req 48. return browse(request, app, model, var_dict, False)
File "/home/gleal/cbengine/engine/wsgi/openshift/../openshift/subviews/browse.py" in _browse 32. custom_vars['TableColspan'] = len(obj.Vars.get('VIEW_ORDER', {}))
File "/home/gleal/cbengine/engine/wsgi/openshift/../openshift/models.py" in Vars 40. curr_vals[field.name] = getattr(self, field.name) File "/home/gleal/cbengine/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in get 343. raise self.field.rel.to.DoesNotExist
Exception Type: DoesNotExist at /estoque/estoque/browse/ Exception Value:
Just figured out that the great villain was the @property
decorator on the method Vars
.
It was making django try to get values from the instance of the class, and in some cases, it might trigger some querys on the DB made by the Django's ORM (basically, in cases where i tried to get values from the current instance).
I changed the @property to a @classmethod and everything worked like a charm. Like this:
#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}
Thanks to anyone who tried to help!
链接地址: http://www.djcxy.com/p/55908.html上一篇: 匹配查询不存在
下一篇: Django异常DoesNotExist