抱歉,有一个新手问题。我有以下模型:
class WeightSlip(models.Model):
grossdate = models.DateTimeField(auto_now=False, auto_now_add=False)
grossweight = models.DecimalField(max_digits=6, decimal_places=2, default=0)
taredate = models.DateTimeField(auto_now=False, auto_now_add=False)
tareweight = models.DecimalField(max_digits=6, decimal_places=2, default=0)
vehicle = models.CharField(max_length=12)
@property
def netweight(self):
return self.grossweight - self.tareweight
@property
def slipdate(self):
if self.grossdate > self.taredate:
return grossdate.date()
else:
return taredate.date()
序列化器:
class WeightSlipSerializer(serializers.ModelSerializer):
class Meta:
model = models.WeightSlip
fields = ('grossdate', 'grossweight', 'taredate', 'tareweight', 'slipdate', 'netweight', 'vehicle')
read_only_fields = ('slipdate', 'netweight')
我正在尝试使用django-rest-framework-过滤器 https://github.com/philipn/django-rest-framework-filters过滤计算出的“netweight”和“slipdate”属性:
class WeightSlipFilter(FilterSet):
class Meta:
model = WeightSlip
fields = ('slipdate', 'netweight', 'vehicle')
这给了我一个错误:
TypeError: 'Meta.fields' contains fields that are not defined on this FilterSet: slipdate, netweight
除了将计算字段添加到数据库之外,是否有解决此问题的方法?
提前致谢。