{"id":6438,"date":"2023-01-18T02:23:34","date_gmt":"2023-01-18T02:23:34","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=6438"},"modified":"2023-01-18T02:27:56","modified_gmt":"2023-01-18T02:27:56","slug":"django-exists","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/django-tutorial\/django-exists\/","title":{"rendered":"Django exists"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Django <code><code>QuerySet<\/code><\/code> <code>exists()<\/code> method to check if a <code><code>QuerySet<\/code><\/code> contains any rows.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-django-queryset-exists-method'>Introduction to the Django QuerySet exists() method <a href=\"#introduction-to-the-django-queryset-exists-method\" class=\"anchor\" id=\"introduction-to-the-django-queryset-exists-method\" title=\"Anchor for Introduction to the Django QuerySet exists() method\">#<\/a><\/h2>\n\n\n\n<p>Sometimes, you want to check if a query contains any rows. To do it, you use the <code>exists()<\/code> method of the <code>QuerySet<\/code> object.<\/p>\n\n\n\n<p>The <code>exists()<\/code> method returns <code>True<\/code> if the <code>QuerySet<\/code> contains any rows or <code>False<\/code> otherwise.<\/p>\n\n\n\n<p>Behind the scenes, the <code>exists()<\/code> will attempt to perform the query in the fastest way possible. However, the query will be nearly identical to a regular <code>QuerySet<\/code> query.<\/p>\n\n\n\n<p>Suppose you have a <code>QuerySet<\/code> and want to check if it has any objects. Instead of doing this:<\/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\"><span class=\"hljs-keyword\">if<\/span> query_set:\n   print(<span class=\"hljs-string\">'the queryset has at least one object'<\/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>&#8230;you should use the <code>exists()<\/code> because it is a little bit faster:<\/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\"><span class=\"hljs-keyword\">if<\/span> query_set.exists():\n   print(<span class=\"hljs-string\">'the queryset has at least one object'<\/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>Django 4.1 added the <code>a<code>exists()<\/code><\/code> which is an asynchronous version of <code>exists()<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='django-exists-method-example'>Django exists() method example <a href=\"#django-exists-method-example\" class=\"anchor\" id=\"django-exists-method-example\" title=\"Anchor for Django exists() method example\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll use the <code>Employee<\/code> model for the demonstration. The <code>Employee<\/code> model maps to the <code>hr_employee<\/code> table in the database:<\/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>First, run the <code>shell_plus<\/code> command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">python manage.py shell_plus<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, find the employees whose first names start with the letter <code>J<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">&gt;&gt;&gt; Employee.objects.filter(first_name__startswith='J').exists()\n<span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-string\">\"a\"<\/span>\n  <span class=\"hljs-keyword\">FROM<\/span> <span class=\"hljs-string\">\"hr_employee\"<\/span>\n <span class=\"hljs-keyword\">WHERE<\/span> <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"first_name\"<\/span>::<span class=\"hljs-built_in\">text<\/span> <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'J%'<\/span>\n <span class=\"hljs-keyword\">LIMIT<\/span> <span class=\"hljs-number\">1<\/span>\nExecution <span class=\"hljs-built_in\">time<\/span>: <span class=\"hljs-number\">0.000000<\/span>s &#91;<span class=\"hljs-keyword\">Database<\/span>: <span class=\"hljs-keyword\">default<\/span>]\n<span class=\"hljs-literal\">True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that Django generated the SQL based on the PostgreSQL. If you use other databases, you may see a slightly different SQL statement.<\/p>\n\n\n\n<p>In this example, the <code>exists()<\/code> method returns True. It selects only the first row to determine whether the <code>QuerySet<\/code> contains any row.<\/p>\n\n\n\n<p>If you do not use the <code>exists()<\/code> method, the <code>QuerySet<\/code> will get all the rows from the <code>hr_employee<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">&gt;&gt;&gt; qs = Employee.objects.filter(first_name__startswith='J') \n&gt;&gt;&gt; print(qs.query)\n<span class=\"hljs-keyword\">SELECT<\/span> <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<span class=\"hljs-keyword\">FROM<\/span> <span class=\"hljs-string\">\"hr_employee\"<\/span>\n<span class=\"hljs-keyword\">WHERE<\/span> <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"first_name\"<\/span>::<span class=\"hljs-built_in\">text<\/span> <span class=\"hljs-keyword\">LIKE<\/span> J%\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"first_name\"<\/span> <span class=\"hljs-keyword\">ASC<\/span>,\n         <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"last_name\"<\/span> <span class=\"hljs-keyword\">ASC<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/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>Use the Django <code>exists()<\/code> method to check if a <code>QuerySet<\/code> contains any rows.<\/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=\"6438\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-exists\/\"\n\t\t\t\tdata-post-title=\"Django exists\"\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=\"6438\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-exists\/\"\n\t\t\t\tdata-post-title=\"Django exists\"\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 use the Django exists() method of a QuerySet to check if a QuerySet contains any rows.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":5531,"menu_order":34,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6438","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/6438","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=6438"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/6438\/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=6438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}