1

プロファイルの写真フィールドと電子メールフィールドを持つユーザーリソースの例を考えてみましょう。ユーザーは他のユーザーのプロフィール写真を見ることができますが、ユーザーは自分のメールアドレスしか見ることができません。

認証されたユーザーに基づいて除外フィールドのセットを変更できるように、tastypieを設定することは可能ですか?

別のアプローチは、完全なユーザーリソースと制限されたユーザーリソースを別々に作成することです。しかし、今のところ、ユーザー認証に基づいてフィールドを制限するアプローチが、tastypieでも実行可能かどうかを知りたいだけです。

また、除外する必要はありません。同じように、要求しているユーザーに基づいてフィールドプロパティを変更する方法はありますか?

4

3 に答える 3

0

除外されたフィールドをエレガントな方法で写真に戻すことができるとは思いません。get_object_listリソースにを含めることで、何らかのオブジェクト操作を使用して、リクエストに基づいてオブジェクト リストを操作できる可能性があります。

apply_limitsただし、カスタム認可クラスでメソッドを使用する方がはるかに適切で論理的です。

于 2012-07-26T01:56:07.300 に答える
0

必要な ModelResource インスタンスに複数継承できる ModelResource サブクラスを作成しました。お気に入り:

class ResourceThatNeedsToExcludeSomeFields(ExcludeResource, ModelResource):
    pass

GET パラメータ (「&exclude=username」など) を介して除外するフィールドを受け取ります。

class ExcludeResource(ModelResource):
"""
Generic class to implement exclusion of fields in all the ModelResource classes
All ModelResource classes will be muliple inherited by this class, whose dehydrate method has been overriden
"""
# STACK: How to exclude some fields from a resource list created by tastypie?
def dehydrate(self, bundle):
    # I was taking the exclude fields from get paramaters as a comma separated value
    if bundle.request.GET.get('exclude'):
        exclude_fields = bundle.request.GET.get('exclude').split(',')
        for field in exclude_fields:
            if str(field) in bundle.data.keys():
                del bundle.data[str(field)]
    return bundle

これを変更して、次のようにユーザー グループ (またはフィールドの基準となる条件) に基づいて除外フィールドを取得できます。

class ExcludeResource(ModelResource):
"""
Generic class to implement exclusion of fields in all the ModelResource classes
All ModelResource classes will be muliple inherited by this class, whose dehydrate method has been overriden
"""
# STACK: How to exclude some fields from a resource list created by tastypie?
def dehydrate(self, bundle):
    exclude_fields = <get a comma separated list of fields to be excluded based in bundle.request.user>
    for field in exclude_fields:
        if str(field) in bundle.data.keys():
            del bundle.data[str(field)]
    return bundle

ただし、このスニペットは、「full=True」で指定された関連リソースでは機能しません

于 2014-02-14T07:34:37.933 に答える
0

はい、それを行う方法があります。電子メールをユーザーのフィールドではなく、別のフィールドに定義する場合(それで機能する可能性がありますが、機能しませんでした)。bundle.request に現在のリクエストが含まれている dehydrate_email を定義すると、それを取得できます。フィールドとして完全に除外されるわけではありませんが、他のフィールドでは None になります。

于 2012-07-28T00:10:05.510 に答える