mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 03:23:59 +03:00
Merge pull request #1711 from kdazzle/ModelViewSet-queryset-static-property
Issue #1707: Add documentation to api-docs.viewsets
This commit is contained in:
commit
8244c7cc33
|
@ -43,6 +43,12 @@ For more complex cases you might also want to override various methods on the vi
|
|||
return 20
|
||||
return 100
|
||||
|
||||
def list(self, request):
|
||||
# Note the use of `get_queryset()` instead of `self.queryset`
|
||||
queryset = self.get_queryset()
|
||||
serializer = UserSerializer(queryset, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
For very simple cases you might want to pass through any class attributes using the `.as_view()` method. For example, your URLconf might include something the following entry.
|
||||
|
||||
url(r'^/users/', ListCreateAPIView.as_view(model=User), name='user-list')
|
||||
|
@ -63,7 +69,7 @@ Each of the concrete generic views provided is built by combining `GenericAPIVie
|
|||
|
||||
The following attributes control the basic view behavior.
|
||||
|
||||
* `queryset` - The queryset that should be used for returning objects from this view. Typically, you must either set this attribute, or override the `get_queryset()` method.
|
||||
* `queryset` - The queryset that should be used for returning objects from this view. Typically, you must either set this attribute, or override the `get_queryset()` method. If you are overriding a view method, it is important that you call `get_queryset()` instead of accessing this property directly, as `queryset` will get evaluated once, and those results will be cached for all subsequent requests.
|
||||
* `serializer_class` - The serializer class that should be used for validating and deserializing input, and for serializing output. Typically, you must either set this attribute, or override the `get_serializer_class()` method.
|
||||
* `lookup_field` - The model field that should be used to for performing object lookup of individual model instances. Defaults to `'pk'`. Note that when using hyperlinked APIs you'll need to ensure that *both* the API views *and* the serializer classes set the lookup fields if you need to use a custom value.
|
||||
* `lookup_url_kwarg` - The URL keyword argument that should be used for object lookup. The URL conf should include a keyword argument corresponding to this value. If unset this defaults to using the same value as `lookup_field`.
|
||||
|
@ -93,6 +99,8 @@ The following attributes are used to control pagination when used with list view
|
|||
|
||||
Returns the queryset that should be used for list views, and that should be used as the base for lookups in detail views. Defaults to returning the queryset specified by the `queryset` attribute, or the default queryset for the model if the `model` shortcut is being used.
|
||||
|
||||
This method should always be used rather than accessing `self.queryset` directly, as `self.queryset` gets evaluated only once, and those results are cached for all subsequent requests.
|
||||
|
||||
May be overridden to provide dynamic behavior such as returning a queryset that is specific to the user making the request.
|
||||
|
||||
For example:
|
||||
|
|
|
@ -43,6 +43,10 @@ class GenericAPIView(views.APIView):
|
|||
|
||||
# You'll need to either set these attributes,
|
||||
# or override `get_queryset()`/`get_serializer_class()`.
|
||||
# If you are overriding a view method, it is important that you call
|
||||
# `get_queryset()` instead of accessing the `queryset` property directly,
|
||||
# as `queryset` will get evaluated only once, and those results are cached
|
||||
# for all subsequent requests.
|
||||
queryset = None
|
||||
serializer_class = None
|
||||
|
||||
|
@ -258,6 +262,10 @@ class GenericAPIView(views.APIView):
|
|||
This must be an iterable, and may be a queryset.
|
||||
Defaults to using `self.queryset`.
|
||||
|
||||
This method should always be used rather than accessing `self.queryset`
|
||||
directly, as `self.queryset` gets evaluated only once, and those results
|
||||
are cached for all subsequent requests.
|
||||
|
||||
You may want to override this if you need to provide different
|
||||
querysets depending on the incoming request.
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user