2016-09-11 5 views
3

私は混乱しています。 ViewSetのソースコードを見てみると、ビューセット内のクエリーセットを定義しないで、クエリーセット取得関数をオーバーライドして、必要なクエリーセットを取得できるように見えます。しかし、私のコードは次のエラーで失敗します。Django restフレームワークは、get_querysetをオーバーライドしてクエリーセット属性を定義できませんか?

AssertionError: `base_name` argument not specified, and could not automatically determine the name from the viewset, as it does not have a `.queryset` attribute. 

は、だから私は、クエリセットの属性をオーバーライドしても、私はまだこの作品...初めにいくつかの偽の属性に設定する必要がありますが、それはクエリセットを定義するために奇妙な感じそれからもう一度それをオーバーライドしてください。

base_name - The base to use for the URL names that are created. If unset the basename will be automatically generated based on the queryset attribute of the viewset, if it has one. Note that if the viewset does not include a queryset attribute then you must set base_name when registering the viewset.

答えて

3

A DRF ModelViewSetは、URLベースを導出するquerysetを使用しています。それができないautomatically generate a basename for the viewsetので

class StudyQuestion(viewsets.ReadOnlyModelViewSet): 
    queryset = Model.objects.all() 
    serializer_class = ModelSerializer 
    permission_classes = (permissions.IsAuthenticated,) 

    def get_queryset(self): 
     """""" 
     return Model.objects.order_by('-word__frequency') 
2

DRFルータは、不平を言っています。 querysetプロパティが設定されていない場合、ベースを宣言するためにルータを登録するときに、オプションのbase_nameプロパティを使用するようDRFに要求されます。

DRFのドキュメントで、このページをチェックしてください:

http://www.django-rest-framework.org/api-guide/routers/

関連する問題