简体   繁体   中英

django: queryset or filter on decimal field

I'm trying to filter some objects on a queryset, field on what I'm trying is a Decimal...

model:

class CheckingAccount(models.Model):
    owner = models.ForeignKey(Owner)
    name = models.CharField(max_length=50, blank=False)
    description = models.CharField(max_length=50, blank=True)
    datetime = models.DateTimeField(default=datetime.now, blank=False)
    currency = models.ForeignKey(Currency)
    balance = models.DecimalField(max_digits=20, decimal_places=6)

    def __unicode__(self):
        return unicode(self.balance)

form:

def clean(self):
    message = self.cleaned_data['acquisition_value']
    ow = Owner.objects.get(user_id=self.request.user.id)
    ca = CheckingAccount.objects.filter(balance=self.cleaned_data['checkingaccounts'])

The error I'm getting is: Exception Value:
Cannot convert to Decimal

Any idea?

It might go wrong for several reasons, depending on cleaned_data returned value.

First - what if cleaned_data- fails and returns and exception or something like that ? your filter won't work :( Second - It might be a decimal casting issue.

IMHO Do not use cleaned_data in the filter. Do your clean elsewhere, and filter with clear easy decimal number in the clean method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM