python - Tastypie annotation error "empty attribute..." -
i'm using tastypie create api , i'm stuck trying annotate sum total of decimals on type. each transaction has associated bucket type , i'd group bucket type, , sum transactions.
api resource
class transactiontotalresource(modelresource): class meta: queryset = ttransaction.objects.values('bucket').annotate(bucket_total=sum('amount')) resource_name = 'transaction_total' include_resource_uri = false allowed_methods = ['get'] authorization= authorization()
model
class ttransaction(models.model): bucket = models.foreignkey(tbucket) date = models.datefield() transaction_type_id = models.integerfield() amount = models.decimalfield(null=true, max_digits=18, decimal_places=2, blank=true) account_id = models.integerfield() recurrence_flag = models.smallintegerfield(null=true) notes = models.charfield(null=true,max_length=100) paid = models.smallintegerfield(null=true) is_credit = models.smallintegerfield(null=true) reconciled = models.smallintegerfield(null=true) active = models.smallintegerfield() class meta: db_table = u't_transaction' ordering = ['-date']
if run terminal, works.
from django.db.models import sum ttransaction.objects.values('bucket').annotate(bucket_total=sum('amount')) [{'bucket': 10, 'bucket_total': decimal('35.24')}, {'bucket': 2, 'bucket_total': decimal('62.00')}]
however, hitting transaction_total url , get
{"error": "the object '{'bucket': 10, 'bucket_total': decimal('35.24')}' has empty attribute 'account_id' , doesn't allow default or null value."}
if set model fields "null=true", different error:
{"error_message": "invalid literal int() base 10: ''", "traceback" ...
is there way tastypie work annotaion?
thought answer since found way in dehydrate method:
def dehydrate(self, bundle): transaction = ttransaction.objects.filter(bucket_id=bundle.data['id']).values('bucket').order_by('bucket').annotate(sum('amount')) bundle.data['transaction_total'] = transaction return bundle
Comments
Post a Comment