generic.Listview不显示在DRF查看器的URL列表中

时间:2017-04-09 21:30:34

标签: python django

我错过了什么,但我不知道它是什么。当我转到DRF查看器时,警报未列在可能的URL列表中。所有其他Rest URL都可以。

这是我的serializer.py:

class OptionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Options
        fields = '__all__'

        validators = [
            UniqueTogetherValidator(
                queryset=Options.objects.all(),
                fields=('Member', 'skey', 'Time_Period')
            )
        ]

api.py:

class OptionViewSet(generics.ListCreateAPIView):
    serializer_class = OptionSerializer

    def get_queryset(self):
        """
        This view should return a list of all the options
        for the currently authenticated user.
        """
        user = self.request.user
        return Options.objects.filter(Member=user)

和我的urls.py:

router = routers.DefaultRouter()
router.register(r'users', api.UserViewSet)
router.register(r'groups', api.GroupViewSet)
router.register(r'currency', api.BitCoinViewSet)
router.register(r'latest_prices', api.CurrencyLatestViewSet)
router.register(r'options', api.OptionViewSet.as_view, 'alerts')

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^api/', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))

]

为什么alert网址没有显示?感谢。

1 个答案:

答案 0 :(得分:3)

路由器仅适用于ViewSet,但您的OptionViewSet是普通APIView

你应该能够通过使用适当的mixins和基类来解决它:

class OptionViewSet(mixins.CreateModelMixin,
                    mixins.ListModelMixin,
                    viewsets.GenericViewSet):

    serializer_class = OptionSerializer

    def get_queryset(self):
        """
        This view should return a list of all the options
        for the currently authenticated user.
        """
        user = self.request.user
        return Options.objects.filter(Member=user)