2013-11-17 22:26:41 +04:00
<!DOCTYPE html>
< html lang = "en" >
2014-11-25 19:04:38 +03:00
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
< meta charset = "utf-8" >
2015-05-15 11:19:49 +03:00
< title > Generic views - Django REST framework< / title >
2014-11-25 19:04:38 +03:00
< link href = "../../img/favicon.ico" rel = "icon" type = "image/x-icon" >
2018-10-11 16:48:33 +03:00
< link rel = "canonical" href = "https://www.django-rest-framework.org/api-guide/generic-views/" / >
2014-11-25 19:04:38 +03:00
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< meta name = "description" content = "Django, API, REST, Generic views" >
< meta name = "author" content = "Tom Christie" >
<!-- Le styles -->
< link href = "../../css/prettify.css" rel = "stylesheet" >
< link href = "../../css/bootstrap.css" rel = "stylesheet" >
< link href = "../../css/bootstrap-responsive.css" rel = "stylesheet" >
< link href = "../../css/default.css" rel = "stylesheet" >
<!-- Le HTML5 shim, for IE6 - 8 support of HTML5 elements -->
<!-- [if lt IE 9]>
< script src = "http://html5shim.googlecode.com/svn/trunk/html5.js" > < / script >
<![endif]-->
< script type = "text/javascript" >
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18852272-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
< / script >
< style >
2016-05-26 13:48:39 +03:00
#sidebarInclude img {
margin-bottom: 10px;
}
#sidebarInclude a.promo {
2014-11-25 19:04:38 +03:00
color: black;
2016-05-26 13:48:39 +03:00
}
2014-11-25 19:04:38 +03:00
@media (max-width: 767px) {
div.promo {
display: none;
}
}
< / style >
< / head >
< body onload = "prettyPrint()" class = "-page" >
2013-11-17 22:26:41 +04:00
< div class = "wrapper" >
2014-11-25 19:04:38 +03:00
< div class = "navbar navbar-inverse navbar-fixed-top" >
2013-11-17 22:26:41 +04:00
< div class = "navbar-inner" >
< div class = "container-fluid" >
2017-05-12 19:15:00 +03:00
< a class = "repo-link btn btn-primary btn-small" href = "https://github.com/encode/django-rest-framework/tree/master" > GitHub< / a >
2015-06-04 17:37:22 +03:00
< a class = "repo-link btn btn-inverse btn-small " rel = "prev" href = "../viewsets/" >
2014-11-25 19:04:38 +03:00
Next < i class = "icon-arrow-right icon-white" > < / i >
< / a >
2015-06-04 17:37:22 +03:00
< a class = "repo-link btn btn-inverse btn-small " rel = "next" href = "../views/" >
2014-11-25 19:04:38 +03:00
< i class = "icon-arrow-left icon-white" > < / i > Previous
< / a >
2015-06-04 17:37:22 +03:00
< a id = "search_modal_show" class = "repo-link btn btn-inverse btn-small" href = "#mkdocs_search_modal" data-toggle = "modal" data-target = "#mkdocs_search_modal" > < i class = "icon-search icon-white" > < / i > Search< / a >
2013-11-17 22:26:41 +04:00
< a class = "btn btn-navbar" data-toggle = "collapse" data-target = ".nav-collapse" >
< span class = "icon-bar" > < / span >
< span class = "icon-bar" > < / span >
< span class = "icon-bar" > < / span >
< / a >
2018-10-11 16:48:33 +03:00
< a class = "brand" href = "https://www.django-rest-framework.org/" > Django REST framework< / a >
2013-11-17 22:26:41 +04:00
< div class = "nav-collapse collapse" >
2014-11-25 19:04:38 +03:00
<!-- Main navigation -->
< ul class = "nav navbar-nav" >
2015-06-04 17:37:22 +03:00
< li >
< a href = "../.." > Home< / a >
< / li >
2013-11-17 22:26:41 +04:00
< li class = "dropdown" >
2014-11-25 19:31:00 +03:00
< a href = "#" class = "dropdown-toggle" data-toggle = "dropdown" > Tutorial < b class = "caret" > < / b > < / a >
2013-11-17 22:26:41 +04:00
< ul class = "dropdown-menu" >
2014-11-25 19:04:38 +03:00
< li >
2015-06-04 17:37:22 +03:00
< a href = "../../tutorial/quickstart/" > Quickstart< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../../tutorial/1-serialization/" > 1 - Serialization< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../../tutorial/2-requests-and-responses/" > 2 - Requests and responses< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../../tutorial/3-class-based-views/" > 3 - Class based views< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../../tutorial/4-authentication-and-permissions/" > 4 - Authentication and permissions< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../../tutorial/5-relationships-and-hyperlinked-apis/" > 5 - Relationships and hyperlinked APIs< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../../tutorial/6-viewsets-and-routers/" > 6 - Viewsets and routers< / a >
2014-11-25 19:04:38 +03:00
< / li >
2016-07-14 15:05:57 +03:00
< li >
< a href = "../../tutorial/7-schemas-and-client-libraries/" > 7 - Schemas and client libraries< / a >
< / li >
2013-11-17 22:26:41 +04:00
< / ul >
< / li >
2014-11-25 19:04:38 +03:00
< li class = "dropdown active" >
2013-11-17 22:26:41 +04:00
< a href = "#" class = "dropdown-toggle" data-toggle = "dropdown" > API Guide < b class = "caret" > < / b > < / a >
< ul class = "dropdown-menu" >
2014-11-25 19:04:38 +03:00
< li >
2015-06-04 17:37:22 +03:00
< a href = "../requests/" > Requests< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../responses/" > Responses< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../views/" > Views< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li class = "active" >
2015-06-04 17:37:22 +03:00
< a href = "./" > Generic views< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../viewsets/" > Viewsets< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../routers/" > Routers< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../parsers/" > Parsers< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../renderers/" > Renderers< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../serializers/" > Serializers< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../fields/" > Serializer fields< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../relations/" > Serializer relations< / a >
2014-11-25 19:04:38 +03:00
< / li >
2014-12-01 15:20:07 +03:00
< li >
2015-06-04 17:37:22 +03:00
< a href = "../validators/" > Validators< / a >
2014-12-01 15:20:07 +03:00
< / li >
2014-11-25 19:04:38 +03:00
< li >
2015-06-04 17:37:22 +03:00
< a href = "../authentication/" > Authentication< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../permissions/" > Permissions< / a >
2014-11-25 19:04:38 +03:00
< / li >
2018-10-11 16:48:33 +03:00
< li >
< a href = "../caching/" > Caching< / a >
< / li >
2014-11-25 19:04:38 +03:00
< li >
2015-06-04 17:37:22 +03:00
< a href = "../throttling/" > Throttling< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../filtering/" > Filtering< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../pagination/" > Pagination< / a >
2014-11-25 19:04:38 +03:00
< / li >
2015-03-06 15:05:16 +03:00
< li >
2015-06-04 17:37:22 +03:00
< a href = "../versioning/" > Versioning< / a >
2015-03-06 15:05:16 +03:00
< / li >
2014-11-25 19:04:38 +03:00
< li >
2015-06-04 17:37:22 +03:00
< a href = "../content-negotiation/" > Content negotiation< / a >
2014-11-25 19:04:38 +03:00
< / li >
2014-12-18 18:42:42 +03:00
< li >
2015-06-04 17:37:22 +03:00
< a href = "../metadata/" > Metadata< / a >
2014-12-18 18:42:42 +03:00
< / li >
2016-07-07 11:41:32 +03:00
< li >
< a href = "../schemas/" > Schemas< / a >
< / li >
2014-11-25 19:04:38 +03:00
< li >
2015-06-04 17:37:22 +03:00
< a href = "../format-suffixes/" > Format suffixes< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../reverse/" > Returning URLs< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../exceptions/" > Exceptions< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../status-codes/" > Status codes< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../testing/" > Testing< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../settings/" > Settings< / a >
2014-11-25 19:04:38 +03:00
< / li >
2013-11-17 22:26:41 +04:00
< / ul >
< / li >
2014-11-25 19:04:38 +03:00
2013-11-17 22:26:41 +04:00
< li class = "dropdown" >
< a href = "#" class = "dropdown-toggle" data-toggle = "dropdown" > Topics < b class = "caret" > < / b > < / a >
< ul class = "dropdown-menu" >
2014-11-25 19:04:38 +03:00
< li >
2015-06-04 17:37:22 +03:00
< a href = "../../topics/documenting-your-api/" > Documenting your API< / a >
2014-11-25 19:04:38 +03:00
< / li >
2016-07-07 11:41:32 +03:00
< li >
< a href = "../../topics/api-clients/" > API Clients< / a >
< / li >
2015-03-06 15:05:16 +03:00
< li >
2015-06-04 17:37:22 +03:00
< a href = "../../topics/internationalization/" > Internationalization< / a >
2015-03-06 15:05:16 +03:00
< / li >
2014-11-25 19:04:38 +03:00
< li >
2015-06-04 17:37:22 +03:00
< a href = "../../topics/ajax-csrf-cors/" > AJAX, CSRF & CORS< / a >
2014-11-25 19:04:38 +03:00
< / li >
2015-10-28 14:35:39 +03:00
< li >
< a href = "../../topics/html-and-forms/" > HTML & Forms< / a >
< / li >
2014-11-25 19:04:38 +03:00
< li >
2015-06-04 17:37:22 +03:00
< a href = "../../topics/browser-enhancements/" > Browser Enhancements< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../../topics/browsable-api/" > The Browsable API< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2015-06-04 17:37:22 +03:00
< a href = "../../topics/rest-hypermedia-hateoas/" > REST, Hypermedia & HATEOAS< / a >
2014-11-25 19:04:38 +03:00
< / li >
2018-10-11 16:48:33 +03:00
< / ul >
< / li >
< li class = "dropdown" >
< a href = "#" class = "dropdown-toggle" data-toggle = "dropdown" > Community < b class = "caret" > < / b > < / a >
< ul class = "dropdown-menu" >
2014-11-25 19:04:38 +03:00
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/tutorials-and-resources/" > Tutorials and Resources< / a >
2017-02-25 22:59:44 +03:00
< / li >
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/third-party-packages/" > Third Party Packages< / a >
2014-11-25 19:04:38 +03:00
< / li >
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/contributing/" > Contributing to REST framework< / a >
2014-11-25 19:04:38 +03:00
< / li >
2014-12-18 16:49:50 +03:00
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/project-management/" > Project management< / a >
2014-12-18 16:49:50 +03:00
< / li >
2017-03-09 17:59:14 +03:00
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/release-notes/" > Release Notes< / a >
2017-03-09 17:59:14 +03:00
< / li >
2014-11-25 19:04:38 +03:00
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/3.8-announcement/" > 3.8 Announcement< / a >
2014-11-25 19:04:38 +03:00
< / li >
2014-12-01 15:20:07 +03:00
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/3.7-announcement/" > 3.7 Announcement< / a >
2014-12-01 15:20:07 +03:00
< / li >
2015-08-06 16:31:52 +03:00
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/3.6-announcement/" > 3.6 Announcement< / a >
2015-08-06 16:31:52 +03:00
< / li >
2015-10-28 14:35:39 +03:00
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/3.5-announcement/" > 3.5 Announcement< / a >
2015-10-28 14:35:39 +03:00
< / li >
2016-07-14 15:05:57 +03:00
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/3.4-announcement/" > 3.4 Announcement< / a >
2016-07-14 15:05:57 +03:00
< / li >
2016-10-20 18:31:37 +03:00
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/3.3-announcement/" > 3.3 Announcement< / a >
2016-10-20 18:31:37 +03:00
< / li >
2017-03-09 17:59:14 +03:00
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/3.2-announcement/" > 3.2 Announcement< / a >
2017-03-09 17:59:14 +03:00
< / li >
2017-10-06 15:06:25 +03:00
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/3.1-announcement/" > 3.1 Announcement< / a >
2017-10-06 15:06:25 +03:00
< / li >
2018-04-03 16:54:40 +03:00
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/3.0-announcement/" > 3.0 Announcement< / a >
2018-04-03 16:54:40 +03:00
< / li >
2014-11-25 19:04:38 +03:00
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/kickstarter-announcement/" > Kickstarter Announcement< / a >
2014-11-25 19:04:38 +03:00
< / li >
2016-05-26 13:48:39 +03:00
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/mozilla-grant/" > Mozilla Grant< / a >
2016-05-26 13:48:39 +03:00
< / li >
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/funding/" > Funding< / a >
2016-05-26 13:48:39 +03:00
< / li >
2014-11-25 19:04:38 +03:00
< li >
2018-10-11 16:48:33 +03:00
< a href = "../../community/jobs/" > Jobs< / a >
2014-11-25 19:04:38 +03:00
< / li >
2013-11-17 22:26:41 +04:00
< / ul >
< / li >
2014-11-25 19:04:38 +03:00
2013-11-17 22:26:41 +04:00
< / ul >
2014-11-25 19:04:38 +03:00
< / div >
<!-- /.nav - collapse -->
2013-11-17 22:26:41 +04:00
< / div >
< / div >
< / div >
< div class = "body-content" >
< div class = "container-fluid" >
2014-11-25 19:04:38 +03:00
<!-- Search Modal -->
2015-06-04 17:37:22 +03:00
< div id = "mkdocs_search_modal" class = "modal hide fade" tabindex = "-1" role = "dialog" aria-labelledby = "myModalLabel" aria-hidden = "true" >
2014-11-25 19:04:38 +03:00
< div class = "modal-header" >
< button type = "button" class = "close" data-dismiss = "modal" aria-hidden = "true" > × < / button >
< h3 id = "myModalLabel" > Documentation search< / h3 >
< / div >
< div class = "modal-body" >
2015-08-06 16:31:52 +03:00
< form role = "form" autocomplete = "off" >
2015-06-04 17:37:22 +03:00
< div class = "form-group" >
< input type = "text" name = "q" class = "form-control" placeholder = "Search..." id = "mkdocs-search-query" >
< / div >
< / form >
< div id = "mkdocs-search-results" > < / div >
2014-11-25 19:04:38 +03:00
< / div >
< div class = "modal-footer" >
< button class = "btn" data-dismiss = "modal" aria-hidden = "true" > Close< / button >
< / div >
< / div >
2013-11-17 22:26:41 +04:00
< div class = "row-fluid" >
< div class = "span3" >
< div id = "table-of-contents" >
< ul class = "nav nav-list side-nav well sidebar-nav-fixed" >
2014-11-25 19:04:38 +03:00
< li class = "main" >
< a href = "#generic-views" > Generic views< / a >
< / li >
< li >
< a href = "#examples" > Examples< / a >
< / li >
< li class = "main" >
< a href = "#api-reference" > API Reference< / a >
< / li >
< li >
< a href = "#genericapiview" > GenericAPIView< / a >
< / li >
< li class = "main" >
< a href = "#mixins" > Mixins< / a >
< / li >
< li >
< a href = "#listmodelmixin" > ListModelMixin< / a >
< / li >
< li >
< a href = "#createmodelmixin" > CreateModelMixin< / a >
< / li >
< li >
< a href = "#retrievemodelmixin" > RetrieveModelMixin< / a >
< / li >
< li >
< a href = "#updatemodelmixin" > UpdateModelMixin< / a >
< / li >
< li >
< a href = "#destroymodelmixin" > DestroyModelMixin< / a >
< / li >
< li class = "main" >
< a href = "#concrete-view-classes" > Concrete View Classes< / a >
< / li >
< li >
< a href = "#createapiview" > CreateAPIView< / a >
< / li >
< li >
< a href = "#listapiview" > ListAPIView< / a >
< / li >
< li >
< a href = "#retrieveapiview" > RetrieveAPIView< / a >
< / li >
< li >
< a href = "#destroyapiview" > DestroyAPIView< / a >
< / li >
< li >
< a href = "#updateapiview" > UpdateAPIView< / a >
< / li >
< li >
< a href = "#listcreateapiview" > ListCreateAPIView< / a >
< / li >
< li >
< a href = "#retrieveupdateapiview" > RetrieveUpdateAPIView< / a >
< / li >
< li >
< a href = "#retrievedestroyapiview" > RetrieveDestroyAPIView< / a >
< / li >
< li >
< a href = "#retrieveupdatedestroyapiview" > RetrieveUpdateDestroyAPIView< / a >
< / li >
< li class = "main" >
< a href = "#customizing-the-generic-views" > Customizing the generic views< / a >
< / li >
< li >
< a href = "#creating-custom-mixins" > Creating custom mixins< / a >
< / li >
< li >
< a href = "#creating-custom-base-classes" > Creating custom base classes< / a >
< / li >
< li class = "main" >
< a href = "#put-as-create" > PUT as create< / a >
< / li >
< li class = "main" >
< a href = "#third-party-packages" > Third party packages< / a >
< / li >
< li >
< a href = "#django-rest-framework-bulk" > Django REST Framework bulk< / a >
< / li >
2015-08-06 16:31:52 +03:00
< li >
< a href = "#django-rest-multiple-models" > Django Rest Multiple Models< / a >
< / li >
2014-11-25 19:04:38 +03:00
2016-05-26 13:48:39 +03:00
< div class = "promo" >
< hr / >
< div id = "sidebarInclude" >
< / div >
2014-11-25 19:04:38 +03:00
< / ul >
2014-01-03 17:26:35 +04:00
2013-11-17 22:26:41 +04:00
< / div >
< / div >
< div id = "main-content" class = "span9" >
2014-11-25 19:04:38 +03:00
2015-06-04 17:37:22 +03:00
2018-10-15 19:32:09 +03:00
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/m" >
< span class = "label label-info" > m< / span >
2015-06-04 17:37:22 +03:00
< / a >
2018-10-15 19:32:09 +03:00
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/i" >
< span class = "label label-info" > i< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/x" >
< span class = "label label-info" > x< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/i" >
< span class = "label label-info" > i< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/n" >
< span class = "label label-info" > n< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/s" >
< span class = "label label-info" > s< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/." >
< span class = "label label-info" > .< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/p" >
< span class = "label label-info" > p< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/y" >
< span class = "label label-info" > y< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/ " >
< span class = "label label-info" > < / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/g" >
< span class = "label label-info" > g< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/e" >
< span class = "label label-info" > e< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/n" >
< span class = "label label-info" > n< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/e" >
< span class = "label label-info" > e< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/r" >
< span class = "label label-info" > r< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/i" >
< span class = "label label-info" > i< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/c" >
< span class = "label label-info" > c< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/s" >
< span class = "label label-info" > s< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/." >
< span class = "label label-info" > .< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/p" >
< span class = "label label-info" > p< / span >
< / a >
< a class = "github" href = "https://github.com/encode/django-rest-framework/tree/master/rest_framework/y" >
< span class = "label label-info" > y< / span >
2015-06-04 17:37:22 +03:00
< / a >
2014-11-25 19:04:38 +03:00
2015-11-04 17:59:23 +03:00
< h1 id = "generic-views" > < a class = "toclink" href = "#generic-views" > Generic views< / a > < / h1 >
2013-11-17 22:26:41 +04:00
< blockquote >
< p > 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.< / p >
2016-12-09 19:07:34 +03:00
< p > — < a href = "https://docs.djangoproject.com/en/stable/ref/class-based-views/#base-vs-generic-views" > Django Documentation< / a > < / p >
2013-11-17 22:26:41 +04:00
< / blockquote >
2016-07-14 15:05:57 +03:00
< p > 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.< / p >
2013-11-17 22:26:41 +04:00
< p > The generic views provided by REST framework allow you to quickly build API views that map closely to your database models.< / p >
< p > If the generic views don't suit the needs of your API, you can drop down to using the regular < code > APIView< / code > class, or reuse the mixins and base classes used by the generic views to compose your own set of reusable generic views.< / p >
2015-11-04 17:59:23 +03:00
< h2 id = "examples" > < a class = "toclink" href = "#examples" > Examples< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > Typically when using the generic views, you'll override the view, and set several class attributes.< / p >
2014-11-25 19:04:38 +03:00
< pre > < code > from django.contrib.auth.models import User
2013-11-17 22:26:41 +04:00
from myapp.serializers import UserSerializer
from rest_framework import generics
from rest_framework.permissions import IsAdminUser
class UserList(generics.ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
permission_classes = (IsAdminUser,)
< / code > < / pre >
< p > For more complex cases you might also want to override various methods on the view class. For example.< / p >
2014-11-25 19:04:38 +03:00
< pre > < code > class UserList(generics.ListCreateAPIView):
2013-11-17 22:26:41 +04:00
queryset = User.objects.all()
serializer_class = UserSerializer
permission_classes = (IsAdminUser,)
2014-08-20 14:09:41 +04:00
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)
2013-11-17 22:26:41 +04:00
< / code > < / pre >
2014-08-28 20:33:56 +04:00
< p > For very simple cases you might want to pass through any class attributes using the < code > .as_view()< / code > method. For example, your URLconf might include something like the following entry:< / p >
2015-05-14 12:03:42 +03:00
< pre > < code > url(r'^/users/', ListCreateAPIView.as_view(queryset=User.objects.all(), serializer_class=UserSerializer), name='user-list')
2013-11-17 22:26:41 +04:00
< / code > < / pre >
< hr / >
2015-11-04 17:59:23 +03:00
< h1 id = "api-reference" > < a class = "toclink" href = "#api-reference" > API Reference< / a > < / h1 >
< h2 id = "genericapiview" > < a class = "toclink" href = "#genericapiview" > GenericAPIView< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > This class extends REST framework's < code > APIView< / code > class, adding commonly required behavior for standard list and detail views.< / p >
< p > Each of the concrete generic views provided is built by combining < code > GenericAPIView< / code > , with one or more mixin classes.< / p >
2015-11-04 17:59:23 +03:00
< h3 id = "attributes" > < a class = "toclink" href = "#attributes" > Attributes< / a > < / h3 >
2013-11-17 22:26:41 +04:00
< p > < strong > Basic settings< / strong > :< / p >
< p > The following attributes control the basic view behavior.< / p >
< ul >
2014-08-20 14:09:41 +04:00
< li > < code > queryset< / code > - The queryset that should be used for returning objects from this view. Typically, you must either set this attribute, or override the < code > get_queryset()< / code > method. If you are overriding a view method, it is important that you call < code > get_queryset()< / code > instead of accessing this property directly, as < code > queryset< / code > will get evaluated once, and those results will be cached for all subsequent requests.< / li >
2013-11-17 22:26:41 +04:00
< li > < code > serializer_class< / code > - 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 < code > get_serializer_class()< / code > method.< / li >
< li > < code > lookup_field< / code > - The model field that should be used to for performing object lookup of individual model instances. Defaults to < code > 'pk'< / code > . Note that when using hyperlinked APIs you'll need to ensure that < em > both< / em > the API views < em > and< / em > the serializer classes set the lookup fields if you need to use a custom value.< / li >
< li > < code > lookup_url_kwarg< / code > - 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 < code > lookup_field< / code > .< / li >
< / ul >
< p > < strong > Pagination< / strong > :< / p >
< p > The following attributes are used to control pagination when used with list views.< / p >
< ul >
2017-03-09 17:59:14 +03:00
< li > < code > pagination_class< / code > - The pagination class that should be used when paginating list results. Defaults to the same value as the < code > DEFAULT_PAGINATION_CLASS< / code > setting, which is < code > 'rest_framework.pagination.PageNumberPagination'< / code > . Setting < code > pagination_class=None< / code > will disable pagination on this view.< / li >
2013-11-17 22:26:41 +04:00
< / ul >
< p > < strong > Filtering< / strong > :< / p >
< ul >
< li > < code > filter_backends< / code > - A list of filter backend classes that should be used for filtering the queryset. Defaults to the same value as the < code > DEFAULT_FILTER_BACKENDS< / code > setting.< / li >
< / ul >
2015-11-04 17:59:23 +03:00
< h3 id = "methods" > < a class = "toclink" href = "#methods" > Methods< / a > < / h3 >
2013-11-17 22:26:41 +04:00
< p > < strong > Base methods< / strong > :< / p >
2015-11-04 17:59:23 +03:00
< h4 id = "get_querysetself" > < a class = "toclink" href = "#get_querysetself" > < code > get_queryset(self)< / code > < / a > < / h4 >
2015-02-11 01:56:05 +03:00
< p > 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 < code > queryset< / code > attribute.< / p >
2014-08-20 14:09:41 +04:00
< p > This method should always be used rather than accessing < code > self.queryset< / code > directly, as < code > self.queryset< / code > gets evaluated only once, and those results are cached for all subsequent requests.< / p >
2014-08-28 20:33:56 +04:00
< p > May be overridden to provide dynamic behavior, such as returning a queryset, that is specific to the user making the request.< / p >
2013-11-17 22:26:41 +04:00
< p > For example:< / p >
2014-11-25 19:04:38 +03:00
< pre > < code > def get_queryset(self):
2013-11-17 22:26:41 +04:00
user = self.request.user
return user.accounts.all()
< / code > < / pre >
2015-11-04 17:59:23 +03:00
< h4 id = "get_objectself" > < a class = "toclink" href = "#get_objectself" > < code > get_object(self)< / code > < / a > < / h4 >
2013-11-17 22:26:41 +04:00
< p > Returns an object instance that should be used for detail views. Defaults to using the < code > lookup_field< / code > parameter to filter the base queryset.< / p >
2014-08-28 20:33:56 +04:00
< p > May be overridden to provide more complex behavior, such as object lookups based on more than one URL kwarg.< / p >
2013-11-17 22:26:41 +04:00
< p > For example:< / p >
2014-11-25 19:04:38 +03:00
< pre > < code > def get_object(self):
2013-11-17 22:26:41 +04:00
queryset = self.get_queryset()
filter = {}
for field in self.multiple_lookup_fields:
filter[field] = self.kwargs[field]
obj = get_object_or_404(queryset, **filter)
self.check_object_permissions(self.request, obj)
return obj
< / code > < / pre >
2014-02-20 18:55:10 +04:00
< p > Note that if your API doesn't include any object level permissions, you may optionally exclude the < code > self.check_object_permissions< / code > , and simply return the object from the < code > get_object_or_404< / code > lookup.< / p >
2015-11-04 17:59:23 +03:00
< h4 id = "filter_querysetself-queryset" > < a class = "toclink" href = "#filter_querysetself-queryset" > < code > filter_queryset(self, queryset)< / code > < / a > < / h4 >
2017-12-20 17:34:31 +03:00
< p > Given a queryset, filter it with whichever filter backends are in use, returning a new queryset.< / p >
< p > For example:< / p >
2015-06-23 14:05:53 +03:00
< pre > < code > def filter_queryset(self, queryset):
filter_backends = (CategoryFilter,)
if 'geo_route' in self.request.query_params:
filter_backends = (GeoRouteFilter, CategoryFilter)
elif 'geo_point' in self.request.query_params:
filter_backends = (GeoPointFilter, CategoryFilter)
2013-11-17 22:26:41 +04:00
2015-06-23 14:05:53 +03:00
for backend in list(filter_backends):
queryset = backend().filter_queryset(self.request, queryset, view=self)
return queryset
2013-11-17 22:26:41 +04:00
< / code > < / pre >
2015-11-04 17:59:23 +03:00
< h4 id = "get_serializer_classself" > < a class = "toclink" href = "#get_serializer_classself" > < code > get_serializer_class(self)< / code > < / a > < / h4 >
2015-02-11 01:56:05 +03:00
< p > Returns the class that should be used for the serializer. Defaults to returning the < code > serializer_class< / code > attribute.< / p >
2014-08-28 20:33:56 +04:00
< p > May be overridden to provide dynamic behavior, such as using different serializers for read and write operations, or providing different serializers to different types of users.< / p >
2013-11-17 22:26:41 +04:00
< p > For example:< / p >
2014-11-25 19:04:38 +03:00
< pre > < code > def get_serializer_class(self):
2013-11-17 22:26:41 +04:00
if self.request.user.is_staff:
return FullAccountSerializer
return BasicAccountSerializer
< / code > < / pre >
2014-12-01 15:20:07 +03:00
< p > < strong > Save and deletion hooks< / strong > :< / p >
< p > The following methods are provided by the mixin classes, and provide easy overriding of the object save or deletion behavior.< / p >
2013-11-17 22:26:41 +04:00
< ul >
2014-12-01 15:20:07 +03:00
< li > < code > perform_create(self, serializer)< / code > - Called by < code > CreateModelMixin< / code > when saving a new object instance.< / li >
< li > < code > perform_update(self, serializer)< / code > - Called by < code > UpdateModelMixin< / code > when saving an existing object instance.< / li >
< li > < code > perform_destroy(self, instance)< / code > - Called by < code > DestroyModelMixin< / code > when deleting an object instance.< / li >
2013-11-17 22:26:41 +04:00
< / ul >
2014-12-01 15:20:07 +03:00
< p > 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.< / p >
< pre > < code > def perform_create(self, serializer):
serializer.save(user=self.request.user)
< / code > < / pre >
< p > 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.< / p >
< pre > < code > def perform_update(self, serializer):
instance = serializer.save()
send_email_confirmation(user=self.request.user, modified=instance)
2013-11-17 22:26:41 +04:00
< / code > < / pre >
2015-08-06 16:31:52 +03:00
< p > You can also use these hooks to provide additional validation, by raising a < code > ValidationError()< / code > . This can be useful if you need some validation logic to apply at the point of database save. For example:< / p >
< pre > < code > def perform_create(self, serializer):
queryset = SignupRequest.objects.filter(user=self.request.user)
if queryset.exists():
raise ValidationError('You have already signed up')
serializer.save(user=self.request.user)
< / code > < / pre >
2014-12-01 15:20:07 +03:00
< p > < strong > Note< / strong > : These methods replace the old-style version 2.x < code > pre_save< / code > , < code > post_save< / code > , < code > pre_delete< / code > and < code > post_delete< / code > methods, which are no longer available.< / p >
2013-11-17 22:26:41 +04:00
< p > < strong > Other methods< / strong > :< / p >
< p > 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 < code > GenericAPIView< / code > .< / p >
< ul >
< li > < code > get_serializer_context(self)< / code > - Returns a dictionary containing any extra context that should be supplied to the serializer. Defaults to including < code > 'request'< / code > , < code > 'view'< / code > and < code > 'format'< / code > keys.< / li >
2015-08-06 16:31:52 +03:00
< li > < code > get_serializer(self, instance=None, data=None, many=False, partial=False)< / code > - Returns a serializer instance.< / li >
2015-05-14 12:03:42 +03:00
< li > < code > get_paginated_response(self, data)< / code > - Returns a paginated style < code > Response< / code > object.< / li >
2013-11-17 22:26:41 +04:00
< li > < code > paginate_queryset(self, queryset)< / code > - Paginate a queryset if required, either returning a page object, or < code > None< / code > if pagination is not configured for this view.< / li >
< li > < code > filter_queryset(self, queryset)< / code > - Given a queryset, filter it with whichever filter backends are in use, returning a new queryset.< / li >
< / ul >
< hr / >
2015-11-04 17:59:23 +03:00
< h1 id = "mixins" > < a class = "toclink" href = "#mixins" > Mixins< / a > < / h1 >
2014-08-28 20:33:56 +04:00
< p > The mixin classes provide the actions that are used to provide the basic view behavior. Note that the mixin classes provide action methods rather than defining the handler methods, such as < code > .get()< / code > and < code > .post()< / code > , directly. This allows for more flexible composition of behavior.< / p >
2015-01-10 12:41:12 +03:00
< p > The mixin classes can be imported from < code > rest_framework.mixins< / code > .< / p >
2015-11-04 17:59:23 +03:00
< h2 id = "listmodelmixin" > < a class = "toclink" href = "#listmodelmixin" > ListModelMixin< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > Provides a < code > .list(request, *args, **kwargs)< / code > method, that implements listing a queryset.< / p >
< p > If the queryset is populated, this returns a < code > 200 OK< / code > response, with a serialized representation of the queryset as the body of the response. The response data may optionally be paginated.< / p >
2015-11-04 17:59:23 +03:00
< h2 id = "createmodelmixin" > < a class = "toclink" href = "#createmodelmixin" > CreateModelMixin< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > Provides a < code > .create(request, *args, **kwargs)< / code > method, that implements creating and saving a new model instance.< / p >
< p > If an object is created this returns a < code > 201 Created< / code > response, with a serialized representation of the object as the body of the response. If the representation contains a key named < code > url< / code > , then the < code > Location< / code > header of the response will be populated with that value.< / p >
< p > If the request data provided for creating the object was invalid, a < code > 400 Bad Request< / code > response will be returned, with the error details as the body of the response.< / p >
2015-11-04 17:59:23 +03:00
< h2 id = "retrievemodelmixin" > < a class = "toclink" href = "#retrievemodelmixin" > RetrieveModelMixin< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > Provides a < code > .retrieve(request, *args, **kwargs)< / code > method, that implements returning an existing model instance in a response.< / p >
< p > If an object can be retrieved this returns a < code > 200 OK< / code > response, with a serialized representation of the object as the body of the response. Otherwise it will return a < code > 404 Not Found< / code > .< / p >
2015-11-04 17:59:23 +03:00
< h2 id = "updatemodelmixin" > < a class = "toclink" href = "#updatemodelmixin" > UpdateModelMixin< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > Provides a < code > .update(request, *args, **kwargs)< / code > method, that implements updating and saving an existing model instance.< / p >
< p > Also provides a < code > .partial_update(request, *args, **kwargs)< / code > method, which is similar to the < code > update< / code > method, except that all fields for the update will be optional. This allows support for HTTP < code > PATCH< / code > requests.< / p >
< p > If an object is updated this returns a < code > 200 OK< / code > response, with a serialized representation of the object as the body of the response.< / p >
< p > If the request data provided for updating the object was invalid, a < code > 400 Bad Request< / code > response will be returned, with the error details as the body of the response.< / p >
2015-11-04 17:59:23 +03:00
< h2 id = "destroymodelmixin" > < a class = "toclink" href = "#destroymodelmixin" > DestroyModelMixin< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > Provides a < code > .destroy(request, *args, **kwargs)< / code > method, that implements deletion of an existing model instance.< / p >
< p > If an object is deleted this returns a < code > 204 No Content< / code > response, otherwise it will return a < code > 404 Not Found< / code > .< / p >
< hr / >
2015-11-04 17:59:23 +03:00
< h1 id = "concrete-view-classes" > < a class = "toclink" href = "#concrete-view-classes" > Concrete View Classes< / a > < / h1 >
2013-11-17 22:26:41 +04:00
< p > The following classes are the concrete generic views. If you're using generic views this is normally the level you'll be working at unless you need heavily customized behavior.< / p >
2015-01-10 12:41:12 +03:00
< p > The view classes can be imported from < code > rest_framework.generics< / code > .< / p >
2015-11-04 17:59:23 +03:00
< h2 id = "createapiview" > < a class = "toclink" href = "#createapiview" > CreateAPIView< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > Used for < strong > create-only< / strong > endpoints.< / p >
< p > Provides a < code > post< / code > method handler.< / p >
< p > Extends: < a href = "#genericapiview" > GenericAPIView< / a > , < a href = "#createmodelmixin" > CreateModelMixin< / a > < / p >
2015-11-04 17:59:23 +03:00
< h2 id = "listapiview" > < a class = "toclink" href = "#listapiview" > ListAPIView< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > Used for < strong > read-only< / strong > endpoints to represent a < strong > collection of model instances< / strong > .< / p >
< p > Provides a < code > get< / code > method handler.< / p >
< p > Extends: < a href = "#genericapiview" > GenericAPIView< / a > , < a href = "#listmodelmixin" > ListModelMixin< / a > < / p >
2015-11-04 17:59:23 +03:00
< h2 id = "retrieveapiview" > < a class = "toclink" href = "#retrieveapiview" > RetrieveAPIView< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > Used for < strong > read-only< / strong > endpoints to represent a < strong > single model instance< / strong > .< / p >
< p > Provides a < code > get< / code > method handler.< / p >
< p > Extends: < a href = "#genericapiview" > GenericAPIView< / a > , < a href = "#retrievemodelmixin" > RetrieveModelMixin< / a > < / p >
2015-11-04 17:59:23 +03:00
< h2 id = "destroyapiview" > < a class = "toclink" href = "#destroyapiview" > DestroyAPIView< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > Used for < strong > delete-only< / strong > endpoints for a < strong > single model instance< / strong > .< / p >
< p > Provides a < code > delete< / code > method handler.< / p >
< p > Extends: < a href = "#genericapiview" > GenericAPIView< / a > , < a href = "#destroymodelmixin" > DestroyModelMixin< / a > < / p >
2015-11-04 17:59:23 +03:00
< h2 id = "updateapiview" > < a class = "toclink" href = "#updateapiview" > UpdateAPIView< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > Used for < strong > update-only< / strong > endpoints for a < strong > single model instance< / strong > .< / p >
< p > Provides < code > put< / code > and < code > patch< / code > method handlers.< / p >
< p > Extends: < a href = "#genericapiview" > GenericAPIView< / a > , < a href = "#updatemodelmixin" > UpdateModelMixin< / a > < / p >
2015-11-04 17:59:23 +03:00
< h2 id = "listcreateapiview" > < a class = "toclink" href = "#listcreateapiview" > ListCreateAPIView< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > Used for < strong > read-write< / strong > endpoints to represent a < strong > collection of model instances< / strong > .< / p >
< p > Provides < code > get< / code > and < code > post< / code > method handlers.< / p >
< p > Extends: < a href = "#genericapiview" > GenericAPIView< / a > , < a href = "#listmodelmixin" > ListModelMixin< / a > , < a href = "#createmodelmixin" > CreateModelMixin< / a > < / p >
2015-11-04 17:59:23 +03:00
< h2 id = "retrieveupdateapiview" > < a class = "toclink" href = "#retrieveupdateapiview" > RetrieveUpdateAPIView< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > Used for < strong > read or update< / strong > endpoints to represent a < strong > single model instance< / strong > .< / p >
< p > Provides < code > get< / code > , < code > put< / code > and < code > patch< / code > method handlers.< / p >
< p > Extends: < a href = "#genericapiview" > GenericAPIView< / a > , < a href = "#retrievemodelmixin" > RetrieveModelMixin< / a > , < a href = "#updatemodelmixin" > UpdateModelMixin< / a > < / p >
2015-11-04 17:59:23 +03:00
< h2 id = "retrievedestroyapiview" > < a class = "toclink" href = "#retrievedestroyapiview" > RetrieveDestroyAPIView< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > Used for < strong > read or delete< / strong > endpoints to represent a < strong > single model instance< / strong > .< / p >
< p > Provides < code > get< / code > and < code > delete< / code > method handlers.< / p >
< p > Extends: < a href = "#genericapiview" > GenericAPIView< / a > , < a href = "#retrievemodelmixin" > RetrieveModelMixin< / a > , < a href = "#destroymodelmixin" > DestroyModelMixin< / a > < / p >
2015-11-04 17:59:23 +03:00
< h2 id = "retrieveupdatedestroyapiview" > < a class = "toclink" href = "#retrieveupdatedestroyapiview" > RetrieveUpdateDestroyAPIView< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > Used for < strong > read-write-delete< / strong > endpoints to represent a < strong > single model instance< / strong > .< / p >
< p > Provides < code > get< / code > , < code > put< / code > , < code > patch< / code > and < code > delete< / code > method handlers.< / p >
< p > Extends: < a href = "#genericapiview" > GenericAPIView< / a > , < a href = "#retrievemodelmixin" > RetrieveModelMixin< / a > , < a href = "#updatemodelmixin" > UpdateModelMixin< / a > , < a href = "#destroymodelmixin" > DestroyModelMixin< / a > < / p >
< hr / >
2015-11-04 17:59:23 +03:00
< h1 id = "customizing-the-generic-views" > < a class = "toclink" href = "#customizing-the-generic-views" > Customizing the generic views< / a > < / h1 >
2013-11-17 22:26:41 +04:00
< p > Often you'll want to use the existing generic views, but use some slightly customized behavior. If you find yourself reusing some bit of customized behavior in multiple places, you might want to refactor the behavior into a common class that you can then just apply to any view or viewset as needed.< / p >
2015-11-04 17:59:23 +03:00
< h2 id = "creating-custom-mixins" > < a class = "toclink" href = "#creating-custom-mixins" > Creating custom mixins< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > For example, if you need to lookup objects based on multiple fields in the URL conf, you could create a mixin class like the following:< / p >
2014-11-25 19:04:38 +03:00
< pre > < code > class MultipleFieldLookupMixin(object):
2013-11-17 22:26:41 +04:00
"""
Apply this mixin to any view or viewset to get multiple field filtering
based on a `lookup_fields` attribute, instead of the default single field filtering.
"""
def get_object(self):
queryset = self.get_queryset() # Get the base queryset
queryset = self.filter_queryset(queryset) # Apply any filter backends
filter = {}
for field in self.lookup_fields:
2016-09-21 14:29:45 +03:00
if self.kwargs[field]: # Ignore empty fields.
filter[field] = self.kwargs[field]
2017-10-06 15:06:25 +03:00
obj = get_object_or_404(queryset, **filter) # Lookup the object
self.check_object_permissions(self.request, obj)
return obj
2013-11-17 22:26:41 +04:00
< / code > < / pre >
< p > You can then simply apply this mixin to a view or viewset anytime you need to apply the custom behavior.< / p >
2014-11-25 19:04:38 +03:00
< pre > < code > class RetrieveUserView(MultipleFieldLookupMixin, generics.RetrieveAPIView):
2013-11-17 22:26:41 +04:00
queryset = User.objects.all()
serializer_class = UserSerializer
lookup_fields = ('account', 'username')
< / code > < / pre >
2014-12-01 15:20:07 +03:00
< p > Using custom mixins is a good option if you have custom behavior that needs to be used.< / p >
2015-11-04 17:59:23 +03:00
< h2 id = "creating-custom-base-classes" > < a class = "toclink" href = "#creating-custom-base-classes" > Creating custom base classes< / a > < / h2 >
2013-11-17 22:26:41 +04:00
< p > 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:< / p >
2014-11-25 19:04:38 +03:00
< pre > < code > class BaseRetrieveView(MultipleFieldLookupMixin,
2013-11-17 22:26:41 +04:00
generics.RetrieveAPIView):
pass
class BaseRetrieveUpdateDestroyView(MultipleFieldLookupMixin,
generics.RetrieveUpdateDestroyAPIView):
pass
< / code > < / pre >
< p > Using custom base classes is a good option if you have custom behavior that consistently needs to be repeated across a large number of views throughout your project.< / p >
2014-11-25 19:04:38 +03:00
< hr / >
2015-11-04 17:59:23 +03:00
< h1 id = "put-as-create" > < a class = "toclink" href = "#put-as-create" > PUT as create< / a > < / h1 >
2014-11-25 19:04:38 +03:00
< p > Prior to version 3.0 the REST framework mixins treated < code > PUT< / code > as either an update or a create operation, depending on if the object already existed or not.< / p >
< p > Allowing < code > PUT< / code > as create operations is problematic, as it necessarily exposes information about the existence or non-existence of objects. It's also not obvious that transparently allowing re-creating of previously deleted instances is necessarily a better default behavior than simply returning < code > 404< / code > responses.< / p >
< p > Both styles "< code > PUT< / code > as 404" and "< code > PUT< / code > as create" can be valid in different circumstances, but from version 3.0 onwards we now use 404 behavior as the default, due to it being simpler and more obvious.< / p >
< p > If you need to generic PUT-as-create behavior you may want to include something like < a href = "https://gist.github.com/tomchristie/a2ace4577eff2c603b1b" > this < code > AllowPUTAsCreateMixin< / code > class< / a > as a mixin to your views.< / p >
< hr / >
2015-11-04 17:59:23 +03:00
< h1 id = "third-party-packages" > < a class = "toclink" href = "#third-party-packages" > Third party packages< / a > < / h1 >
2014-01-20 01:12:37 +04:00
< p > The following third party packages provide additional generic view implementations.< / p >
2015-11-04 17:59:23 +03:00
< h2 id = "django-rest-framework-bulk" > < a class = "toclink" href = "#django-rest-framework-bulk" > Django REST Framework bulk< / a > < / h2 >
2014-01-20 01:12:37 +04:00
< p > The < a href = "https://github.com/miki725/django-rest-framework-bulk" > django-rest-framework-bulk package< / a > implements generic view mixins as well as some common concrete generic views to allow to apply bulk operations via API requests.< / p >
2015-11-04 17:59:23 +03:00
< h2 id = "django-rest-multiple-models" > < a class = "toclink" href = "#django-rest-multiple-models" > Django Rest Multiple Models< / a > < / h2 >
2017-08-22 16:31:16 +03:00
< p > < a href = "https://github.com/MattBroach/DjangoRestMultipleModels" > Django Rest Multiple Models< / a > provides a generic view (and mixin) for sending multiple serialized models and/or querysets via a single API request.< / p >
2015-06-04 17:37:22 +03:00
2015-06-23 14:05:53 +03:00
< / div > <!-- /span -->
< / div > <!-- /row -->
< / div > <!-- /.fluid - container -->
< / div > <!-- /.body content -->
2014-11-25 19:04:38 +03:00
< div id = "push" > < / div >
2015-06-23 14:05:53 +03:00
< / div > <!-- /.wrapper -->
2013-11-17 22:26:41 +04:00
< footer class = "span12" >
2015-10-28 14:35:39 +03:00
< p > Documentation built with < a href = "http://www.mkdocs.org/" > MkDocs< / a > .
2014-11-25 19:04:38 +03:00
< / p >
2013-11-17 22:26:41 +04:00
< / footer >
2014-11-25 19:04:38 +03:00
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
< script src = "../../js/jquery-1.8.1-min.js" > < / script >
< script src = "../../js/prettify-1.0.js" > < / script >
< script src = "../../js/bootstrap-2.1.1-min.js" > < / script >
2016-05-26 13:48:39 +03:00
< script src = "https://fund.django-rest-framework.org/sidebar_include.js" > < / script >
2015-06-04 17:37:22 +03:00
< script > var base _url = '../..' ; < / script >
< script src = "../../mkdocs/js/require.js" > < / script >
2014-12-11 12:55:10 +03:00
< script src = "../../js/theme.js" > < / script >
2014-11-25 19:04:38 +03:00
< script >
var shiftWindow = function() {
scrollBy(0, -50)
};
2015-06-23 14:05:53 +03:00
2014-11-25 19:04:38 +03:00
if (location.hash) shiftWindow();
window.addEventListener("hashchange", shiftWindow);
2013-11-17 22:26:41 +04:00
2014-11-25 19:04:38 +03:00
$('.dropdown-menu').on('click touchstart', function(event) {
event.stopPropagation();
});
2013-11-17 22:26:41 +04:00
2015-06-23 14:05:53 +03:00
// Dynamically force sidenav/dropdown to no higher than browser window
$('.side-nav, .dropdown-menu').css('max-height', window.innerHeight - 130);
2014-11-25 19:04:38 +03:00
$(function() {
$(window).resize(function() {
2015-06-23 14:05:53 +03:00
$('.side-nav, .dropdown-menu').css('max-height', window.innerHeight - 130);
2013-11-17 22:26:41 +04:00
});
2014-11-25 19:04:38 +03:00
});
< / script >
< / body >
< / html >