{"id":6427,"date":"2023-01-13T03:44:48","date_gmt":"2023-01-13T03:44:48","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=6427"},"modified":"2023-08-30T02:01:24","modified_gmt":"2023-08-30T02:01:24","slug":"django-limit-offset","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/django-tutorial\/django-limit-offset\/","title":{"rendered":"Django Limit Offset"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to limit a <code>QuerySet<\/code> in Django, which is equivalent to the SQL&#8217;s <code>LIMIT<\/code> and <code>OFFSET<\/code> clauses.<\/p>\n\n\n\n<p>In practice, you rarely get all the rows from one or more tables in the database. Instead, you get a subset of rows for displaying on a web page.<\/p>\n\n\n\n<p>Django uses the <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-slicing\/\">slicing<\/a> syntax to limit a <code>QuerySet<\/code> to a specific number of objects. Behind the scenes, Django executes a SQL <code>SELECT<\/code> statement with <code>LIMIT<\/code> and <code>OFFSET<\/code> clauses.<\/p>\n\n\n\n<p>Suppose you want to access the first 10 rows in a table, you can use the following slicing:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Entity.objects.all()&#91;:<span class=\"hljs-number\">10<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since the table stores the rows in an unspecified order, the &#8220;first 10 objects&#8221; become unpredictable.<\/p>\n\n\n\n<p>Therefore, you should always <a href=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-order_by\/\">sort the results<\/a> before slicing the query. For example, the following gets the first 10 rows ordered by the <code>field_name<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Entity.objects.all().order_by(field_name)&#91;:<span class=\"hljs-number\">10<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To get the rows from 10 to 20, you can use the following slicing:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Entity.objects.all().order_by(field_name)&#91;<span class=\"hljs-number\">10<\/span>:<span class=\"hljs-number\">20<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In general, the syntax for limiting results is as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Entity.objects.all()&#91;Offset:Offset+Limit]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, the <code>Offset<\/code> is the number of rows you want to skip and the <code>Limit<\/code> is the number of rows you want to retrieve.<\/p>\n\n\n\n<p>Note that Django doesn&#8217;t support negative indexing like:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Entity.objects.all().order_by(field_name)&#91;<span class=\"hljs-number\">-10<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Also, when you slice a <code><code>QuerySet<\/code><\/code>, Django returns a new <code><code>QuerySet<\/code><\/code>.<\/p>\n\n\n\n<p>To retrieve the first or last row, you should use the <code>first()<\/code> or <code>last()<\/code> method because if the <code>QuerySet<\/code> is empty and you use a slice:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Entity.objects.all().order_by(field_name)&#91;<span class=\"hljs-number\">0<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>&#8230;then you&#8217;ll get an <code>IndexError<\/code> exception.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='django-limit-offset-example'>Django Limit Offset example <a href=\"#django-limit-offset-example\" class=\"anchor\" id=\"django-limit-offset-example\" title=\"Anchor for Django Limit Offset example\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll use the <code>Employee<\/code> model from the <code>HR<\/code> app for the demonstration. The <code>Employee<\/code> model maps to the <code>hr_employee<\/code> table:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"211\" height=\"211\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/01\/django_orm_employee_table.png\" alt=\"\" class=\"wp-image-6379\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/01\/django_orm_employee_table.png 211w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/01\/django_orm_employee_table-150x150.png 150w\" sizes=\"auto, (max-width: 211px) 100vw, 211px\" \/><\/figure>\n\n\n\n<p>The following example gets the first 10 employees ordered by their first names:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-meta\">&gt;&gt;&gt; <\/span>Employee.objects.all().order_by(<span class=\"hljs-string\">'first_name'<\/span>)&#91;:<span class=\"hljs-number\">10<\/span>] \nSELECT <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"id\"<\/span>,\n       <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"first_name\"<\/span>,\n       <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"last_name\"<\/span>,\n       <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"contact_id\"<\/span>,\n       <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"department_id\"<\/span>\n  FROM <span class=\"hljs-string\">\"hr_employee\"<\/span>\n ORDER BY <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"first_name\"<\/span> ASC\n LIMIT <span class=\"hljs-number\">10<\/span>\nExecution time: <span class=\"hljs-number\">0.000000<\/span>s &#91;Database: default]\n&lt;QuerySet &#91;&lt;Employee: Aaron Pearson&gt;, &lt;Employee: Adam Crane&gt;, &lt;Employee: Adam Stewart&gt;, &lt;Employee: Adrienne Green&gt;, &lt;Employee: Alan Johnson&gt;, &lt;Employee: Alexa West&gt;, &lt;Employee: Alicia Wyatt&gt;, &lt;Employee: Amanda Benson&gt;, &lt;Employee: Amber Brown&gt;, &lt;Employee: Amy Lopez&gt;]&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example skips the first 10 rows and gets the next 10 rows from the <code>hr_employee<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-meta\">&gt;&gt;&gt; <\/span>Employee.objects.order_by(<span class=\"hljs-string\">'first_name'<\/span>)&#91;<span class=\"hljs-number\">10<\/span>:<span class=\"hljs-number\">20<\/span>] \nSELECT <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"id\"<\/span>,\n       <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"first_name\"<\/span>,\n       <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"last_name\"<\/span>,\n       <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"contact_id\"<\/span>,\n       <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"department_id\"<\/span>\n  FROM <span class=\"hljs-string\">\"hr_employee\"<\/span>\n ORDER BY <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"first_name\"<\/span> ASC\n LIMIT <span class=\"hljs-number\">10<\/span>\nOFFSET <span class=\"hljs-number\">10<\/span>\nExecution time: <span class=\"hljs-number\">0.001001<\/span>s &#91;Database: default]\n&lt;QuerySet &#91;&lt;Employee: Amy Lee&gt;, &lt;Employee: Andre Perez&gt;, &lt;Employee: Andrea Mcintosh&gt;, &lt;Employee: Andrew Dixon&gt;, &lt;Employee: Andrew Guerra&gt;, &lt;Employee: Ann Chang&gt;, \n&lt;Employee: Anne Odom&gt;, &lt;Employee: Anthony Fuentes&gt;, &lt;Employee: Anthony Welch&gt;, &lt;Employee: Ashley Brown&gt;]&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Django uses array-slicing syntax to limit the number of objects returned by a <code>QuerySet<\/code>.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"6427\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-limit-offset\/\"\n\t\t\t\tdata-post-title=\"Django Limit Offset\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"6427\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-limit-offset\/\"\n\t\t\t\tdata-post-title=\"Django Limit Offset\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to limit a QuerySet in Django, which is equivalent to the SQL&#8217;s LIMIT and OFFSET clauses.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":5531,"menu_order":30,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6427","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/6427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=6427"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/6427\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5531"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=6427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}