カスタムロジックをどこに置くことができますか、またはtastypieにカスタムロジックを追加するにはどの関数をオーバーロードする必要がありますか. 例:大文字CustomObject
を含むものを返したいが、返すname
前に小文字にしたい。
2 に答える
1
そのようなカスタムのものを提供したい場合は、 の間に行うことをお勧めしdehydrate
ます。documentationを確認してください。例は、あなたが探しているもののほとんどです:
class MyResource(ModelResource):
# The ``title`` field is already added to the class by ``ModelResource``
# and populated off ``Note.title``. But we want allcaps titles...
class Meta:
queryset = Note.objects.all()
def dehydrate_title(self, bundle):
return bundle.data['title'].upper()
もちろん、あなたが探していることを除いて.lower()
:)
class CustomObjectResource(ModelResource):
class Meta:
queryset = CustomObject.objects.all()
def dehydrate_title(self, bundle):
return bundle.data['name'].lower()
于 2013-01-31T14:46:57.587 に答える