{"id":6405,"date":"2023-01-12T09:06:31","date_gmt":"2023-01-12T09:06:31","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=6405"},"modified":"2023-01-12T09:06:32","modified_gmt":"2023-01-12T09:06:32","slug":"django-isnull","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/django-tutorial\/django-isnull\/","title":{"rendered":"Django isnull"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Django <code>isnull<\/code> to check whether a value is <code>NULL<\/code> or not.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-django-isnull'>Introduction to Django isnull <a href=\"#introduction-to-django-isnull\" class=\"anchor\" id=\"introduction-to-django-isnull\" title=\"Anchor for Introduction to Django isnull\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll use the <code>Employee<\/code> and <code>Contact<\/code> models from the <code>HR<\/code> application for the demonstration. The <code>Emloyee<\/code> and <code>Contact<\/code> models map to the <code>hr_employee<\/code> and <code>hr_contact<\/code> tables:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"558\" height=\"211\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/01\/django_orm_employee_contact.png\" alt=\"\" class=\"wp-image-6408\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/01\/django_orm_employee_contact.png 558w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2023\/01\/django_orm_employee_contact-300x113.png 300w\" sizes=\"auto, (max-width: 558px) 100vw, 558px\" \/><\/figure>\n\n\n\n<p>In relational databases, <code><code>NULL<\/code><\/code> denotes missing information. For example, if you don&#8217;t know the contact of an employee, you can use <code><code>NULL<\/code><\/code> for the contact of the employee at the time of recording.<\/p>\n\n\n\n<p><code>NULL<\/code> is special because you cannot use the <code>=<\/code> operator to compare a value with it. Instead, you use the <code>IS<\/code> operator.<\/p>\n\n\n\n<p>For example, to check if the value in the <code>contact_id<\/code> is <code>NULL<\/code> or not, you use the <code>IS<\/code> operator as follows:<\/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\">contact_id IS NULL<\/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>The <code>IS<\/code> <code><code>NULL<\/code><\/code> returns true if the <code>contact_id<\/code> is <code><code>NULL<\/code><\/code> or false otherwise. <\/p>\n\n\n\n<p>To negate the <code>IS<\/code> operator, you can use the <code>NOT<\/code> operator like this:<\/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\">contact_id IS NOT NULL<\/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 uses isnull to check if a value is <code>NUL<\/code> or not:<\/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.filter(field_name__isnull=<span class=\"hljs-literal\">True<\/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>The <code>filter()<\/code> method returns all instances with the <code>field_name<\/code> is <code>NULL<\/code>. To negate the <code>isnull<\/code>, you compare it with <code>False<\/code> instead:<\/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.filter(field_name__isnull=<span class=\"hljs-literal\">False<\/span>)<\/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 case, the <code>filter()<\/code> returns all instances of the Entity whose <code>field_name<\/code> is not <code>NULL<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='django-isnull-example'>Django isnull example <a href=\"#django-isnull-example\" class=\"anchor\" id=\"django-isnull-example\" title=\"Anchor for Django isnull example\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the <code>isnull<\/code> to get employees who do not have contacts:<\/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\"><span class=\"hljs-meta\">&gt;&gt;&gt; <\/span>Employee.objects.filter(contact_id__isnull=<span class=\"hljs-literal\">True<\/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 WHERE <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"contact_id\"<\/span> IS NULL<\/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>The generated query use <code>IS NULL<\/code> operator to compare the <code>contact_id<\/code> with <code>NULL<\/code>.<\/p>\n\n\n\n<p>The following example uses the <code>isnull<\/code> to get all employees who have contacts:<\/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\"><span class=\"hljs-meta\">&gt;&gt;&gt; <\/span>Employee.objects.filter(contact_id__isnull=<span class=\"hljs-literal\">False<\/span>) \r\nSELECT <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"id\"<\/span>,\r\n       <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"first_name\"<\/span>,\r\n       <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"last_name\"<\/span>,\r\n       <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"contact_id\"<\/span>,\r\n       <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"department_id\"<\/span>\r\n  FROM <span class=\"hljs-string\">\"hr_employee\"<\/span>\r\n WHERE <span class=\"hljs-string\">\"hr_employee\"<\/span>.<span class=\"hljs-string\">\"contact_id\"<\/span> IS NOT NULL<\/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>In this case, the generated query uses the <code>IS NOT NULL<\/code> to compare the values in the <code>contact_id<\/code> column with <code>NULL<\/code>.<\/p>\n\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 Django <code>isnull<\/code> to check if a value is <code>NULL<\/code> or not.<\/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=\"6405\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-isnull\/\"\n\t\t\t\tdata-post-title=\"Django isnull\"\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=\"6405\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-isnull\/\"\n\t\t\t\tdata-post-title=\"Django isnull\"\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 isnull to check whether a value is NULL or not.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":5531,"menu_order":37,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6405","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/6405","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=6405"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/6405\/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=6405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}