1

カスタムロジックをどこに置くことができますか、またはtastypieにカスタムロジックを追加するにはどの関数をオーバーロードする必要がありますか. 例:大文字CustomObjectを含むものを返したいが、返すname前に小文字にしたい。

4

2 に答える 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 に答える