From ed93e13a1c6f792e14176bdaa5e96d0fa2c63a2f Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 1 Dec 2014 12:20:07 +0000 Subject: Update documentation --- api-guide/generic-views/index.html | 46 ++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 17 deletions(-) (limited to 'api-guide/generic-views/index.html') diff --git a/api-guide/generic-views/index.html b/api-guide/generic-views/index.html index e39d4118..c0a6ef8c 100644 --- a/api-guide/generic-views/index.html +++ b/api-guide/generic-views/index.html @@ -164,6 +164,10 @@ Serializer relations +
  • + Validators +
  • +
  • Authentication
  • @@ -263,6 +267,10 @@ 2.4 Announcement +
  • + 3.0 Announcement +
  • +
  • Kickstarter Announcement
  • @@ -488,12 +496,15 @@ -

    Generic views

    +
    +

    Note: This is the documentation for the version 3.0 of REST framework. Documentation for version 2.4 is also available.

    +
    +

    Generic views

    Django’s generic views... were developed as a shortcut for common usage patterns... They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to repeat yourself.

    Django Documentation

    -

    One of the key benefits of class based views is the way they allow you to compose bits of reusable behaviour. REST framework takes advantage of this by providing a number of pre-built views that provide for commonly used patterns.

    +

    One of the key benefits of class based views is the way they allow you to compose bits of reusable behavior. REST framework takes advantage of this by providing a number of pre-built views that provide for commonly used patterns.

    The generic views provided by REST framework allow you to quickly build API views that map closely to your database models.

    If the generic views don't suit the needs of your API, you can drop down to using the regular APIView class, or reuse the mixins and base classes used by the generic views to compose your own set of reusable generic views.

    Examples

    @@ -618,22 +629,23 @@ class UserList(generics.ListCreateAPIView): return 20 return 100 -

    Save / deletion hooks:

    -

    The following methods are provided as placeholder interfaces. They contain empty implementations and are not called directly by GenericAPIView, but they are overridden and used by some of the mixin classes.

    +

    Save and deletion hooks:

    +

    The following methods are provided by the mixin classes, and provide easy overriding of the object save or deletion behavior.

    -

    The pre_save method in particular is a useful hook for setting attributes that are implicit in the request, but are not part of the request data. For instance, you might set an attribute on the object based on the request user, or based on a URL keyword argument.

    -
    def pre_save(self, obj):
    -    """
    -    Set the object's owner, based on the incoming request.
    -    """
    -    obj.owner = self.request.user
    +

    These hooks are particularly useful for setting attributes that are implicit in the request, but are not part of the request data. For instance, you might set an attribute on the object based on the request user, or based on a URL keyword argument.

    +
    def perform_create(self, serializer):
    +    serializer.save(user=self.request.user)
    +
    +

    These override points are also particularly useful for adding behavior that occurs before or after saving an object, such as emailing a confirmation, or logging the update.

    +
    def perform_update(self, serializer):
    +    instance = serializer.save()
    +    send_email_confirmation(user=self.request.user, modified=instance)
     
    -

    Remember that the pre_save() method is not called by GenericAPIView itself, but it is called by create() and update() methods on the CreateModelMixin and UpdateModelMixin classes.

    +

    Note: These methods replace the old-style version 2.x pre_save, post_save, pre_delete and post_delete methods, which are no longer available.

    Other methods:

    You won't typically need to override the following methods, although you might need to call into them if you're writing custom views using GenericAPIView.

    -

    Using custom mixins is a good option if you have custom behavior that needs to be used

    +

    Using custom mixins is a good option if you have custom behavior that needs to be used.

    Creating custom base classes

    If you are using a mixin across multiple views, you can take this a step further and create your own set of base views that can then be used throughout your project. For example:

    class BaseRetrieveView(MultipleFieldLookupMixin,
    @@ -765,7 +777,7 @@ class BaseRetrieveUpdateDestroyView(MultipleFieldLookupMixin,
       
     
       
     
    -- 
    cgit v1.2.3