2012-05-18 2 views
15

임 내 admin.py에 연산 결과를 저장하려고 그리고 난이 오류가 :이 평균은?, 내 모든 모델 분야는 무엇퀀 타이즈 결과는 현재 컨텍스트에 너무 많은 숫자를 가지고

quantize result has too many digits for current context

.... 
def save_model(self, request, obj, form, change): 

    usuario_libra = obj.consignee.membresia.libra 
    valores   = Valores.objects.get(pk=1) 
    vtasa   = valores.tasa 
    vaduana   = valores.aduana 
    vgestion  = valores.gestion 
    vfee   = valores.fee 
    vcombustible = valores.combustible 

    trans_aereo  = obj.peso * usuario_libra * vtasa 
    aduana   = (obj.peso * vaduana)*vtasa 
    fee_airpot  = (obj.peso * vfee)*vtasa 
    combustible  = (obj.peso * vcombustible)*vtasa 
    itbis   = (trans_aereo+vgestion)*Decimal(0.16) 
    total   = trans_aereo + vgestion + aduana + fee_airpot + combustible + itbis 

    if not obj.id: 
     obj.total = total 
     ... 

을 진수 어떤 도움을 주시기 바랍니다

당신에게 감사

나는이 (가) 'max_digits'필드 옵션을 증가시켜이 문제를 해결할 수 있었다
+0

[this] (http://stackoverflow.com/questions/2051575/django-combine-models-decimalfield-with-forms-error-quantize-result-has-too)가 도움이됩니까? – okm

+0

@okm 나는 도움을 청하려고 노력하지 않았다. ( – Asinox

답변

20

.

class Myclass(models.Model): 
    my_field = models.DecimalField(max_digits=11, decimal_places=2, blank=True, null=True) 

저장하려는 가장 긴 숫자에 맞게 충분히 크게하십시오.

그 또한 의해 정밀도가 크게 설정해야 할 수도 있습니다 작동하지 않는 경우 :

from decimal import getcontext 
    ... 
    getcontext().prec = 11 

전체 컨텍스트 매개 변수에 대한 파이썬 소수점 설명서를 참조하십시오.

+2

11 자리를 초과하는 다른 코드를 깨뜨릴 수도있는 상황에 따라 컨텍스트가 변경되므로 이상적이지 않다. – Cerin

관련 문제