5

django-pistonを利用してDjangoアプリ用に作成したAPIの仕上げを行っています。RequestAPIは、それぞれまたはIPAddressインスタンスであるリクエストまたはIPアドレスで検索できます。各リクエストには、1つ以上をIPAddress関連付けることができます。

したがって、たとえばIPAddress、「アクティブ」、「非アクティブ」、または「すべて」(いずれか)のアクティビティステータスに一致するすべてのオブジェクトを表示するAPI呼び出しがあります。各インスタンスが関連付けられているは、として使用できRequestます。IPAddressIPAddress.request

私が抱えている問題は、リクエストをプロビジョニングした人Request.inputterのインスタンスへの外部キーであるということです。UserこのAPI呼び出し用に作成したハンドラーから結果が返されると、Userインスタンスのすべてのフィールドが表示されます。これには、が含まれpasswordます。

これは悪いです; これいらない。

これが私のハンドラーです:

class SearchByIPStatusHandler(BaseHandler):
    model = IPAddress
    allowed_methods = ('GET',)
    anonymous = AnonymousIPHandler

    def read(self, request, status):
        """
        Returns IP addresses based on activity status.  
        Status: 'active', 'inactive', 'all'

        """
        if status == 'all':
            return self.model.objects.all()
        else:
            active = True if (status=='active') else False
            return self.model.objects.filter(active=active)

そして、これがからの結果の例です/api/show/all/

<response>
  <resource>
    <updated>2010-02-05 17:08:53.651729</updated>
    <expires>2010-02-12 17:08:23</expires>
    <created>2010-02-05 17:08:53.625318</created>
    <nexthop>255.255.255.255</nexthop>
    <netmask>255.255.255.254</netmask>
    <address>2.4.6.80/31</address>
    <active>True</active>
    <id>4</id>
    <request>
      <updated>2010-02-05 17:08:53.382381</updated>
      <created>2010-02-05 17:08:53.382313</created>
      <expires>2010-02-12 17:08:23</expires>
      <incident>20100212-badthings-01</incident>
      <reason>bad things happened</reason>
      <inputter>
        <username>jathan</username>
        <first_name>Jathan</first_name>
        <last_name>McCollum</last_name>
        <is_active>True</is_active>
        <email>email@fake.notreal</email>
        <is_superuser>True</is_superuser>
        <is_staff>True</is_staff>
        <last_login>2010-02-05 18:55:51.877746</last_login>
        <password>[ENCRYPTED STRING I REDACTED]</password>
        <id>1</id>
        <date_joined>2010-01-28 09:56:32</date_joined>
      </inputter>
      <requester>joeuser</requester>
      <active>True</active>
    </request>
  </resource>
</response>

結果に本当に欲しいのはinputter.username、他のすべてではなく、です。excludeハンドラーに属性を実装するさまざまな反復を試みましたが、役に立ちませんでした。リクエストフィールド全体をスキップすると、次のように正常に機能します。

ハンドラー内:

exclude = ('request', )

その結果:

<response>
  <resource>
    <updated>2010-02-05 17:08:53.651729</updated>
    <expires>2010-02-12 17:08:23</expires>
    <created>2010-02-05 17:08:53.625318</created>
    <nexthop>255.255.255.255</nexthop>
    <netmask>255.255.255.254</netmask>
    <address>2.4.6.80/31</address>
    <active>True</active>
    <id>4</id>
  </resource>
</response>

しかし、これらの結果も私が望むものではありません。

それで、最後に、私の質問:

ネストされたフィールドをハンドラーの結果から除外するにはどうすればよいですか?それも可能ですか?

私は次のさまざまな反復を試しましたが、いずれも結果がないか、意図しない結果になります。

# try to exclude request.inputter
exclude = ( ('request', ('inputter', ), ) )

# try to exclude request.inputter.password
exclude = ( ('request', ('inputter', ('password', ) ) ) ) 

私は、このコンテキストでフィールドの除外が行われる方法を誤解または誤用していると思います。そのため、このトピックに関する啓蒙は大歓迎です。

4

2 に答える 2

3

除外の代わりにインクルードを使用してみてください。例えば

include = (('request', ('inputter', ('username', 'therestofthefields'))))

excludeのように用途が広いと書いたかどうかは覚えていませんinclude

また、django-pistonのGoogleグループでは、ほとんどのことについて話し合っています。そこでこの質問をすることで、より多くの成功を収めることができます。

于 2010-02-07T09:47:47.200 に答える
3

fields =ハンドラーの句を介して目的のフィールドを指定することにより、目的の結果を得ることができます。

外部キーからのモデルフィールドは、次のように指定できます。

('foreign_model_field', ('nested_field1', 'nested_field2'))

あなたの場合、以下が機能するはずです(簡潔にするためにいくつかのフィールドは省略されています):

fields = ('updated', 'expires', 'created', 
    ('request', ('incident', 'reason', ('inputter', ('username',)))))
于 2010-02-09T01:03:37.760 に答える