我与Django的REST的架构工作,我有一个返回我的信息与这样的过滤器的API:Django的API REST返回的所有对象模型中的
http://example.com/api/products?category=clothing&in_stock=True
--this returns me 10 items
但它也返回整个模型数据,如果我不把过滤器,这是默认的方式。
http://example.com/api/products/
--this returns me more than 100 (all the Model Table)
我该如何禁用这个默认操作,我的意思是,我怎样才能使过滤器是必要的,使这个api的作品?甚至更好!我怎样才能让最后一个URL返回一个空的JSON响应?
UPDATE 下面是一些代码:
serializers.py
class OEntradaDetalleSerializer(serializers.HyperlinkedModelSerializer):
item = serializers.RelatedField(source='producto.item')
descripcion = serializers.RelatedField(source='producto.descripcion')
unidad = serializers.RelatedField(source='producto.unidad')
class Meta:
model = OEntradaDetalle
fields = ('url','item','descripcion','unidad','cantidad_ordenada','cantidad_recibida','epc')
views.py
class OEntradaDetalleViewSet(BulkUpdateModelMixin,viewsets.ModelViewSet):
filter_backends = (filters.DjangoFilterBackend,)
filter_fields = ('cantidad_ordenada','cantidad_recibida','oentrada__codigo_proveedor','oentrada__folio')
queryset = OEntradaDetalle.objects.all()
serializer_class = OEntradaDetalleSerializer
urls.py
router2 = BulkUpdateRouter()
router2.register(r'oentradadetalle', OEntradaDetalleViewSet)
urlpatterns = patterns('',
url(r'^api/',include(router2.urls)),
)
网址范例
http://localhost:8000/api/oentradadetalle/?oentrada__folio=E01
THIS RETURNS ONLY SOME FILTERED VALUES
http://localhost:8000/api/oentradadetalle/
THIS RETURNS EVERYTHING IN THE MODEL (I need to remove this or make it return some empty data)
请给一些代码。 – 2014-10-30 21:43:16
我不知道为什么你需要一些代码,它有默认的API行为,我需要重写它。无论如何...有我自己的代码更新,我希望你能帮助我。谢谢 – 2014-10-30 22:28:19