0
我正在使用tastypie創建一個API,並且我被卡住,試圖註釋某個特定類型的小數總和。每個交易都有一個關聯的存儲桶類型,我想按存儲桶類型進行分組,並對這些交易進行求和。Tastypie註釋錯誤「空屬性...」
API資源
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()
模型
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']
,如果我從終端運行它,它的工作原理。
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')}]
然而,擊中transaction_total URL,這和我得到
{"error": "The object '{'bucket': 10, 'bucket_total': Decimal('35.24')}' has an empty attribute 'account_id' and doesn't allow a default or null value."}
如果我把所有的模型字段是「空=真」,然後我得到一個不同的錯誤:
{"error_message": "invalid literal for int() with base 10: ''", "traceback" ...
有沒有辦法讓tastypie和annotaion一起工作?