{"id":329331,"date":"2023-09-28T12:19:37","date_gmt":"2023-09-28T12:19:37","guid":{"rendered":"https:\/\/pythonguides.com\/?p=16383"},"modified":"2023-09-28T12:19:37","modified_gmt":"2023-09-28T12:19:37","slug":"union-operation-on-models-django","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2023\/09\/28\/union-operation-on-models-django\/","title":{"rendered":"Union operation on models Django"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<p>In this&nbsp;<a class=\"rank-math-link\" href=\"https:\/\/pythonguides.com\/how-to-install-django\/\"  rel=\"noreferrer noopener\">Python Django Tutorial<\/a>, I will show you how to perform <strong>union operation on models Django<\/strong>.<\/p>\n<p>You will understand how to perform union operations on querysets that belong to the same model. Also, you will learn how to perform union operations on query sets that belong to different models.<\/p>\n<h2 class=\"wp-block-heading\">Union operation on models Django<\/h2>\n<p>So <strong>&#8216;what is a union operation?&#8217;<\/strong>, from a mathematics point of view, suppose you have two sets <strong>A = [2,3 5,7]<\/strong> and <strong>B = [1,4,5,6]<\/strong>. To perform union between these sets, the symbol <strong>U<\/strong> is used like this <strong>&#8216;A&#8217;<\/strong> <strong>U<\/strong> <strong>&#8216;B&#8217;<\/strong>.<\/p>\n<p>The result of the union is a set <strong>[1,2,3,4,5,6,7]<\/strong> that contains all the elements from sets A and B except the duplicate. <\/p>\n<p>If you see both sets, they have a common element which is 5 but this element is included only one time in the final set after performing the union operation.<\/p>\n<p>Like that Django supports <strong>Set Operations <\/strong>that can be performed on the models. Set Operations are used to get meaningful results from the field stored in the models, under different special conditions.<\/p>\n<p>Now, you will learn one of the Set Operations, called <strong>Union Operation<\/strong>. But where you will use the union operation in Django, well the answer is simple. You will use the union with Django Queryset.<\/p>\n<p>Suppose you have a model related to the blog and you want to fetch the blog based on different categories. Then to combine these two separate queryset into a single queryset, use the union operation.<\/p>\n<p>Now you can say union operation in Django allows us to combine multiple queryset into a single queryset.<\/p>\n<p>The<strong> Union Operation<\/strong> can be applied on the same or different models. The fields and data types should match when querysets are from distinct models or even from the same model.<\/p>\n<p>Also, check: <a href=\"https:\/\/pythonguides.com\/create-model-in-django\/\"  aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">How to Create model in Django<\/a><\/p>\n<h3 class=\"wp-block-heading\">Union Operation on the Same Model Using Django<\/h3>\n<p>I hope that you have a clear understanding of what Union Operation is. Let&#8217;s now turn our attention to the practical implementation of union operations.<\/p>\n<p>Let&#8217;s learn how to use the union operation on the same model<\/p>\n<p>Before starting the demonstration, I&#8217;ll show you the model and explain how to create it. For that open the terminal and create a virtual environment named <strong>&#8216;env_union&#8217;<\/strong>.<\/p>\n<pre class=\"wp-block-code\"><code>python -m venv env_union<\/code><\/pre>\n<p>Then activate the environment using the below code.<\/p>\n<pre class=\"wp-block-code\"><code>env_union\\Scripts\\activate<\/code><\/pre>\n<p>Install the latest version of Django.<\/p>\n<pre class=\"wp-block-code\"><code>pip install django<\/code><\/pre>\n<p>Create a Django project named <strong>&#8216;django_union&#8217;<\/strong> using the below code.<\/p>\n<pre class=\"wp-block-code\"><code>django-admin startproject django_union<\/code><\/pre>\n<p>Change the directory to django_union.<\/p>\n<pre class=\"wp-block-code\"><code>cd django_union<\/code><\/pre>\n<p>Then create a Django app named <strong>&#8216;union_app&#8217;<\/strong> using the below code.<\/p>\n<pre class=\"wp-block-code\"><code>python manage.py startapp union_app<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"691\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Project-Setup-1024x691.jpg\" alt=\"Union Operation on Same Model Using Django Project Setup\" class=\"wp-image-45422\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Project-Setup-1024x691.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Project-Setup-300x202.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Project-Setup-768x518.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Project-Setup.jpg 1359w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Union Operation on Same Model Using Django Project Setup\"><\/figure>\n<\/div>\n<p>Now open the Django project in your preferred IDE such as Visual Studio Code.<\/p>\n<p>Firstly, create the model named <strong>&#8216;Employee&#8217;<\/strong> in the<strong>&nbsp;model.py<\/strong>&nbsp;file of the Django app <strong>&#8216;union_app&#8217;<\/strong> and add the following lines of code.<\/p>\n<pre class=\"wp-block-code\"><code>from django.db import models\n\nclass Employee(models.Model):\n    first_name = models.CharField(max_length=200)\n    last_name = models.CharField(max_length=200)\n    position = models.CharField(max_length=100)\n    age = models.PositiveIntegerField()\n    \n    def __str__(self):  \n        return \"%s %s %s %s\" % (self.first_name, self.last_name, self.position, self.age)  <\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"254\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Employee-Using-Django-1024x254.jpg\" alt=\"Union Operation on Same Model Employee Using Django\" class=\"wp-image-45423\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Employee-Using-Django-1024x254.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Employee-Using-Django-300x74.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Employee-Using-Django-768x191.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Employee-Using-Django.jpg 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Union Operation on Same Model Employee Using Django\"><\/figure>\n<\/div>\n<p>In the above code define the model named <strong>Employee<\/strong> with field lists <strong>first_name<\/strong>, <strong>last_name<\/strong>, position of type <strong>CharField<\/strong> and age of type <strong>PositiveIntegerField<\/strong>.<\/p>\n<p>Next, register the <strong>Employee<\/strong> model by writing the below code in the <strong>admin.py<\/strong> file of your Django app <strong>&#8216;union_app&#8217;<\/strong>.<\/p>\n<pre class=\"wp-block-code\"><code>from django.contrib import admin\nfrom .models import Employee\n\nclass AdminEmployee(admin.ModelAdmin):\n    list_display = &#91;'first_name', 'last_name', 'position', 'age']\n\nadmin.site.register(Employee, AdminEmployee)<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"306\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Registering-Model-1024x306.jpg\" alt=\"Union Operation on Same Model Using Django Registering Model\" class=\"wp-image-45424\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Registering-Model-1024x306.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Registering-Model-300x90.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Registering-Model-768x229.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Registering-Model.jpg 1272w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Union Operation on Same Model Using Django Registering Model\"><\/figure>\n<\/div>\n<p>Include your app in the <strong>INSTALLED_APPS<\/strong> list in your <strong>setting.py<\/strong> file of the Django project <strong>&#8216;django_union&#8217;<\/strong>.<\/p>\n<pre class=\"wp-block-code\"><code>INSTALLED_APPS = &#91;\n    'django.contrib.admin',\n    'django.contrib.auth',\n    'django.contrib.contenttypes',\n    'django.contrib.sessions',\n    'django.contrib.messages',\n    'django.contrib.staticfiles',\n    'union_app'\n]<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"377\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Installing-App-1024x377.jpg\" alt=\"Union Operation on Same Model Using Django Installing App\" class=\"wp-image-45425\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Installing-App-1024x377.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Installing-App-300x111.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Installing-App-768x283.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Installing-App.jpg 1205w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Union Operation on Same Model Using Django Installing App\"><\/figure>\n<\/div>\n<p>Now, run the below commands one by one to make a migration file.<\/p>\n<pre class=\"wp-block-code\"><code>python manage.py makemigrations\npython manage.py migrate<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"626\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Migration-1024x626.jpg\" alt=\"Union Operation on Same Model Using Django Migration\" class=\"wp-image-45426\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Migration-1024x626.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Migration-300x183.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Migration-768x470.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-Same-Model-Using-Django-Migration.jpg 1104w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Union Operation on Same Model Using Django Migration\"><\/figure>\n<\/div>\n<p>Open the Python shell by writing the below command in your terminal.<\/p>\n<pre class=\"wp-block-code\"><code>python manage.py shell<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"139\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-Shell-Union-Operation-on-Same-Model-Using-Django-1024x139.jpg\" alt=\"Python Shell Union Operation on Same Model Using Django\" class=\"wp-image-45427\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-Shell-Union-Operation-on-Same-Model-Using-Django-1024x139.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-Shell-Union-Operation-on-Same-Model-Using-Django-300x41.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-Shell-Union-Operation-on-Same-Model-Using-Django-768x104.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-Shell-Union-Operation-on-Same-Model-Using-Django.jpg 1105w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Python Shell Union Operation on Same Model Using Django\"><\/figure>\n<\/div>\n<p>Now use the below command in your Python shell and create some records related to the Employee.<\/p>\n<pre class=\"wp-block-code\"><code>from union_app.models import Employee\nemp = Employee.objects.create(first_name = \"Maria\", last_name = \"Ted\", position = \"Developer\", age = \"28\")<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"217\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-Shell-Union-Operation-on-Same-Model-Creating-Employee-1024x217.jpg\" alt=\"Python Shell Union Operation on Same Model Creating Employee\" class=\"wp-image-45428\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-Shell-Union-Operation-on-Same-Model-Creating-Employee-1024x217.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-Shell-Union-Operation-on-Same-Model-Creating-Employee-300x63.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-Shell-Union-Operation-on-Same-Model-Creating-Employee-768x162.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Python-Shell-Union-Operation-on-Same-Model-Creating-Employee.jpg 1144w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Python Shell Union Operation on Same Model Creating Employee\"><\/figure>\n<\/div>\n<p>You can use the above code to create multiple employee records in the database.<\/p>\n<p>In this example, you will perform Union Operation on the same model. So write the below code in your terminal.<\/p>\n<pre class=\"wp-block-code\"><code><strong># Import<\/strong>\n\nfrom union_app.models import Employee\n\n<strong># QuerySet<\/strong>\n\nqueryset1 = Employee.objects.filter(age__gt=28)\nqueryset1\n\nqueryset2 = Employee.objects.filter(first_name=\"Maria\")\nqueryset2\n\n<strong># Union Operation<\/strong>\n\nqueryset1.union(queryset2)<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"386\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-models-python-django-1024x386.png\" alt=\"union operation on models python django\" class=\"wp-image-26508\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-models-python-django-1024x386.png 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-models-python-django-300x113.png 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-models-python-django-768x289.png 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-models-python-django.png 1378w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"union operation on models python django\"><\/figure>\n<\/div>\n<p>In the above code first import the model <strong>Employee<\/strong> from the file <strong>models.py<\/strong> of your Django app <strong>&#8216;union_app&#8217;<\/strong>. <\/p>\n<ul>\n<li>Then retrieve all the employees from the database as queryset whose age is greater than 28 using the <strong>Employee.objects.filter(age__gt=28)<\/strong> where <strong>age__gt<\/strong> means <strong>age greater than<\/strong>.<\/li>\n<li>Again retrieve the employee objects whose first name is Maria using the  <strong>Employee.objects.filter(first_name=&#8221;Maria&#8221;)<\/strong><\/li>\n<li>Finally, the code <strong>queryset1.union(queryset2)<\/strong> performs the union operation on <strong>queryset1<\/strong> and <strong>queryset2<\/strong>, and the result of the operation is a new queryset containing all unique <strong>Employee<\/strong> objects that either have an age greater than 28 or a first name equal to Maria.<\/li>\n<\/ul>\n<p>Let&#8217;s take on more examples where you are going to perform Union Operation on Model Fileds <strong>position<\/strong> and <strong>last_name<\/strong>.<\/p>\n<pre class=\"wp-block-code\"><code><strong># Import<\/strong>\n\nfrom union_app.models import Employee\n\n<strong># QuerySet<\/strong>\n\nqueryset1 = Employee.objects.filter(position=\"Vice President\")\nqueryset1\n\nqueryset2 = Employee.objects.filter(last_name=\"Johnson\")\nqueryset2\n\n<strong># Union Operation<\/strong>\n\nqueryset1.union(queryset2)<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"310\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-same-model-using-django-1024x310.png\" alt=\"union operation on same model using django\" class=\"wp-image-26512\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-same-model-using-django-1024x310.png 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-same-model-using-django-300x91.png 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-same-model-using-django-768x233.png 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-same-model-using-django.png 1218w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"union operation on same model using django\"><\/figure>\n<\/div>\n<p>In the above code retrieving the records from the database using the filter() method based on the model field names such as <strong>Position is Vice President and Last_Name is Johnson<\/strong>.<\/p>\n<ul>\n<li>After that storing the retrieved querysets or objects to the corresponding variable <strong>queryset1<\/strong> and <strong>queryset2<\/strong>.<\/li>\n<li>Finally, the code <strong>queryset2.union(queryset1)<\/strong> performs the union operation on <strong>queryset2<\/strong> and <strong>queryset1<\/strong>, and the result of the operation is a new queryset containing all unique <strong>Employee<\/strong> objects that either have position <strong>Vice President<\/strong> or a last name equal to <strong>Johnson<\/strong>.<\/li>\n<\/ul>\n<p>So, if you want to get the duplicate values, use the <strong>all=True<\/strong> parameter with the union operation as shown below.<\/p>\n<pre class=\"wp-block-code\"><code><strong># Union Operation<\/strong>\n\nqueryset1.union(queryset2, all=True)<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"79\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-same-model-using-python-django-1024x79.png\" alt=\"union operation on same model using python django\" class=\"wp-image-26513\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-same-model-using-python-django-1024x79.png 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-same-model-using-python-django-300x23.png 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-same-model-using-python-django-768x59.png 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-same-model-using-python-django.png 1371w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"union operation on same model using python django\"><\/figure>\n<\/div>\n<p>Read: <a href=\"https:\/\/pythonguides.com\/django-vs-reactjs\/\"  aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Python Django vs ReactJS<\/a><\/p>\n<h3 class=\"wp-block-heading\">Union Operation on different models using Django<\/h3>\n<p>Now you know how to perform union operations on queryset that belong to the same model. But in this section, you will learn how to use Union Operation on the different models. To better understand the concept, we&#8217;ll look at some examples.<\/p>\n<p>Before starting the demonstration, I&#8217;ll show you the models and explain how to create them. <\/p>\n<p>Firstly, open the <strong>models.py<\/strong> file of your Django app <strong>&#8216;myApp&#8217;<\/strong> and create a <strong>Customer<\/strong> model by adding the following lines of code.<\/p>\n<pre class=\"wp-block-code\"><code>from django.db import models\n\n# Create your models here.\n\nclass Customer(models.Model):\n    Customer_Name = models.CharField(max_length=200)\n    Customer_Contact_Name = models.CharField(max_length=200)\n    Customer_Country = models.CharField(max_length=100)\n    Customer_City = models.CharField(max_length=100)\n    \n    def __str__(self):  \n        return \"%s %s %s %s\" % (self.Customer_Name, self.Customer_Contact_Name, self.Customer_Country, self.Customer_City) <\/code><\/pre>\n<p>Next, we create a <strong>Supplier<\/strong> model.<\/p>\n<pre class=\"wp-block-code\"><code>from django.db import models\n\n# Create your models here.\n\nclass Supplier(models.Model):\n    Supplier_Name = models.CharField(max_length=200)\n    Supplier_Contact_Name = models.CharField(max_length=200)\n    Supplier_Country = models.CharField(max_length=100)\n    Supplier_City = models.CharField(max_length=100)\n    \n    def __str__(self):  \n        return \"%s %s %s %s\" % (self.Supplier_Name, self.Supplier_Contact_Name, self.Supplier_Country, self.Supplier_City) <\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"297\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-different-models-using-Django-1024x297.jpg\" alt=\"Union Operation on different models using Django\" class=\"wp-image-45463\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-different-models-using-Django-1024x297.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-different-models-using-Django-300x87.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-different-models-using-Django-768x223.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-different-models-using-Django-1536x445.jpg 1536w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-different-models-using-Django.jpg 1659w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Union Operation on different models using Django\"><\/figure>\n<\/div>\n<p>In the above code from lines 12 to 19 define the model named <strong>&#8216; Customer&#8217;<\/strong> that contains the field list <strong>Customer_Name<\/strong>, <strong>Customer_Contact_Name<\/strong>, <strong>Customer_Country<\/strong> and <strong>Customer_City<\/strong> of type<strong> CharField<\/strong>.<\/p>\n<p>After that from lines 21 to 28 define the model <strong>&#8216;Supplier&#8217;<\/strong> with a field list <strong>Supplier_Name<\/strong>, <strong><strong>Supplier<\/strong>_Contact_Name<\/strong>, <strong><strong>Supplier<\/strong>_Country<\/strong> and <strong><strong>Supplier<\/strong>_City<\/strong> of type<strong> CharField<\/strong><\/p>\n<p>After the successful creation of models, register all the models in the <strong>admin.py<\/strong> file of your Django app <strong>&#8216;myApp&#8217;<\/strong>.<\/p>\n<pre class=\"wp-block-code\"><code>from django.contrib import admin\nfrom .models import Customer\nfrom .models import Supplier\n\n\nclass AdminCustomer(admin.ModelAdmin):\n    list_display = &#91;'Customer_Name', 'Customer_Contact_Name', 'Customer_Country', 'Customer_City']\n\nclass AdminSupplier(admin.ModelAdmin):\n    list_display =  &#91;'Supplier_Name', 'Supplier_Contact_Name', 'Supplier_Country', 'Supplier_City']\n\nadmin.site.register(Customer, AdminCustomer)\nadmin.site.register(Supplier, AdminSupplier)\n<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"279\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-different-models-using-Django-Register-Model-1024x279.jpg\" alt=\"Union Operation on different models using Django Register Model\" class=\"wp-image-45465\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-different-models-using-Django-Register-Model-1024x279.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-different-models-using-Django-Register-Model-300x82.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-different-models-using-Django-Register-Model-768x209.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Union-Operation-on-different-models-using-Django-Register-Model.jpg 1420w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Union Operation on different models using Django Register Model\"><\/figure>\n<\/div>\n<p>Now take an example where you will learn how to perform union operations on querysets that belong to different models.<\/p>\n<p>So run the following code in your Python shell.<\/p>\n<pre class=\"wp-block-code\"><code><strong># Import<\/strong>\n\nfrom myApp.models.customer import Customer\nfrom myApp.models.supplier import Supplier\n\n<strong># QuerySet<\/strong>\n\nqueryset1 = Customer.objects.filter(Customer_Country='UK')\nqueryset1\n\nqueryset2 = Supplier.objects.filter(Supplier_Country='UK')\nqueryset2\n\n<strong># Union Operation<\/strong>\n\nqueryset1.union(queryset2)<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"526\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-different-models-using-django-1024x526.png\" alt=\"union operation on different models using django\" class=\"wp-image-26518\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-different-models-using-django-1024x526.png 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-different-models-using-django-300x154.png 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-different-models-using-django-768x395.png 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-different-models-using-django.png 1389w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"union operation on different models using django\"><\/figure>\n<\/div>\n<p>In the above code, line 3 retrieves all the customer objects from the database that belong to the country &#8220;UK&#8221;. Then in line 5 retrieve all the supplier objects from the database that belong to the country <strong>&#8220;UK&#8221;<\/strong>.<\/p>\n<p>Finally, line 7 performs a <strong>union<\/strong> operation on <strong>queryset1<\/strong> and <strong>queryset2<\/strong> to combine the objects of both queryset into a new queryset that contains all the unique customer or supplier objects that have a country equal to <strong>&#8220;UK&#8221;<\/strong>.<\/p>\n<p>Here you should notice that the queryset1 and queryset2 are from the different models.<\/p>\n<p>Let&#8217;s take one more example where you will perform the union operation on querysets that come from different models but are based on the primary key value.<\/p>\n<p>Open the Python Shell and perform the Union Operation on different models.<\/p>\n<pre class=\"wp-block-code\"><code><strong># Import<\/strong>\n\nfrom myApp.models.customer import Customer\nfrom myApp.models.supplier import Supplier\n\n<strong># QuerySet<\/strong>\n\nqueryset1 = Customer.objects.filter(pk__in=&#91;1, 2])\nqueryset1\n\nqueryset2 = Supplier.objects.filter(pk__in=&#91;3])\nqueryset2\n\n<strong># Union Operation<\/strong>\n\nqueryset2.union(queryset1)<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"327\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-different-models-using-python-django-1024x327.png\" alt=\"union operation on different models using python django\" class=\"wp-image-26525\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-different-models-using-python-django-1024x327.png 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-different-models-using-python-django-300x96.png 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-different-models-using-python-django-768x245.png 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/union-operation-on-different-models-using-python-django.png 1380w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"union operation on different models using python django\"><\/figure>\n<\/div>\n<p>The above code does the following things:<\/p>\n<ul>\n<li>Firstly we get the record from the Customer model on the basis of the filter&#8217;s primary key values by using the querysets.<\/li>\n<li>Then, we also get the record from the Supplier model on the basis of the filter primary key values by using the querysets.<\/li>\n<li>Next, we use the Union Operation to get the record by combining both querysets.<\/li>\n<\/ul>\n<p>Read: <a href=\"https:\/\/pythonguides.com\/python-django-format-date\/\"  aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Python Django format date<\/a><\/p>\n<h3 class=\"wp-block-heading\">Limit the selected fields in Union Operation<\/h3>\n<p>In this section, you will learn what we mean by limiting the selected fields while performing Union Operation.<\/p>\n<p>In Django, we can&#8217;t always perform union operations on querysets. The fact that querysets do not have the same columns and datatypes.<\/p>\n<p>However, it&#8217;s possible in some cases that these models have certain common fields. As a result, in this situation, we limit the fields that are picked and then do a union operation. And, to do so we use the Django values_list argument.<\/p>\n<p>Let&#8217;s see an example to understand the concept:<\/p>\n<p>Before we start the demonstration, I&#8217;ll show you the models and explain how to create them. <\/p>\n<p>Firstly, we create the <strong>EmpDetails<\/strong> model.<\/p>\n<pre class=\"wp-block-code\"><code>from django.db import models\n\n# Create your models here.\n\nclass EmpDetails(models.Model):\n    Name = models.CharField(max_length=200)\n    Gender = models.CharField(max_length=20)\n    Position = models.CharField(max_length=100)\n    Country = models.CharField(max_length=100)\n    \n    def __str__(self):  \n        return \"%s %s %s %s\" % (self.Name, self.Gender, self.Position, self.Country)  <\/code><\/pre>\n<p>Next, we create the <strong>EmpSalary<\/strong> model.<\/p>\n<pre class=\"wp-block-code\"><code>from django.db import models\n\n# Create your models here.\n\nclass EmpSalary(models.Model):\n    Name = models.CharField(max_length=200)\n    Gender = models.CharField(max_length=20)\n    Salary = models.FloatField()\n    \n    def __str__(self):  \n        return \"%s %s %s\" % (self.Name, self.Gender, self.Salary)<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"385\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Limit-the-selected-fields-in-Union-Operation-Models-1024x385.jpg\" alt=\"Limit the selected fields in Union Operation Models\" class=\"wp-image-45466\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Limit-the-selected-fields-in-Union-Operation-Models-1024x385.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Limit-the-selected-fields-in-Union-Operation-Models-300x113.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Limit-the-selected-fields-in-Union-Operation-Models-768x289.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Limit-the-selected-fields-in-Union-Operation-Models.jpg 1293w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Limit the selected fields in Union Operation Models\"><\/figure>\n<\/div>\n<p>After the successful creation of models, register the model in the <strong>admin.py<\/strong> file of your Django app <strong>&#8216;myAPP&#8217;<\/strong>.<\/p>\n<pre class=\"wp-block-code\"><code>from django.contrib import admin\nfrom .models import EmpDetails\nfrom .models import EmpSalary\n\n# Register your models here.\n\nclass AdminEmpDetails(admin.ModelAdmin):\n    list_display = &#91;'Name', 'Gender', 'Position', 'Country']\n\nclass AdminEmpSalary(admin.ModelAdmin):\n    list_display = &#91;'Name', 'Gender', 'Salary']\n\nadmin.site.register(EmpDetails, AdminEmpDetails)\nadmin.site.register(EmpSalary, AdminEmpSalary)<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"445\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Limit-the-selected-fields-in-Union-Operation-Register-Models-1024x445.jpg\" alt=\"Limit the selected fields in Union Operation Register Models\" class=\"wp-image-45469\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Limit-the-selected-fields-in-Union-Operation-Register-Models-1024x445.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Limit-the-selected-fields-in-Union-Operation-Register-Models-300x130.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Limit-the-selected-fields-in-Union-Operation-Register-Models-768x334.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/09\/Limit-the-selected-fields-in-Union-Operation-Register-Models.jpg 1038w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Limit the selected fields in Union Operation Register Models\"><\/figure>\n<\/div>\n<p>Now let&#8217;s see with an example how to perform the union operation on querysets that are based limited field.<\/p>\n<p>So run the following code in your Python shell.<\/p>\n<pre class=\"wp-block-code\"><code><strong># Import<\/strong>\n\nfrom myApp.models.empdetails import EmpDetails\nfrom myApp.models.empsalary import EmpSalary\n\n<strong># QuerySet<\/strong>\n\nqueryset1 = EmpDetails.objects.all()\nqueryset1\n\nqueryset2 = EmpSalary.objects.all()\nqueryset2\n\n<strong># Union Operation<\/strong>\n\nqueryset2.union(queryset1)<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/limit-the-selected-fields-in-union-operation-1024x347.png\" alt=\"limit the selected fields in union operation\" class=\"wp-image-26535\" style=\"width:810px;height:274px\" width=\"810\" height=\"274\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/limit-the-selected-fields-in-union-operation-1024x347.png 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/limit-the-selected-fields-in-union-operation-300x102.png 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/limit-the-selected-fields-in-union-operation-768x261.png 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/limit-the-selected-fields-in-union-operation.png 1394w\" sizes=\"(max-width: 810px) 100vw, 810px\" title=\"limit the selected fields in union operation\"><\/figure>\n<\/div>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"50\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/limit-the-selected-fields-in-union-opertion-in-django-1024x50.png\" alt=\"limit the selected fields in union opertion in django\" class=\"wp-image-26536\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/limit-the-selected-fields-in-union-opertion-in-django-1024x50.png 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/limit-the-selected-fields-in-union-opertion-in-django-300x15.png 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/limit-the-selected-fields-in-union-opertion-in-django-768x37.png 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/limit-the-selected-fields-in-union-opertion-in-django.png 1189w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"limit the selected fields in union opertion in django\"><\/figure>\n<\/div>\n<p>In the above code<\/p>\n<ul>\n<li>firstly we get all the records from the EmpDetails model by using the querysets.<\/li>\n<li>Then, we also get all the records from the EmpSalary model by using the querysets.<\/li>\n<li>Next, we use the Union Operation to get all records by combining both querysets. But, we get an error.<\/li>\n<\/ul>\n<p>Now, to resolve this error we can use the <strong>values_list<\/strong> parameter of Django to limit the selected fields and after that perform a union operation.<\/p>\n<pre class=\"wp-block-code\"><code><strong># Import<\/strong>\n\nfrom myApp.models.empdetails import EmpDetails\nfrom myApp.models.empsalary import EmpSalary\n\n<strong># QuerySet<\/strong>\n\nqueryset1 = EmpDetails.objects.values_list(\"Name\", \"Gender\")\nqueryset1\n\nqueryset2 = EmpSalary.objects.values_list(\"Name\",\"Gender\")\nqueryset2\n\n<strong># Union Operation<\/strong>\n\nqueryset2.union(queryset1)<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"330\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/limit-the-selected-fields-in-union-operation-using-python-django-1024x330.png\" alt=\"limit the selected fields in union operation using python django\" class=\"wp-image-26537\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/limit-the-selected-fields-in-union-operation-using-python-django-1024x330.png 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/limit-the-selected-fields-in-union-operation-using-python-django-300x97.png 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/limit-the-selected-fields-in-union-operation-using-python-django-768x248.png 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/limit-the-selected-fields-in-union-operation-using-python-django.png 1370w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"limit the selected fields in union operation using python django\"><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n<p>In this Django Tutorial, we have discussed \u201c<strong>Union operation on models Django<\/strong>\u201d and we have also discussed the following topics in this tutorial.<\/p>\n<p>You may also like to read the following Python Django tutorials.<\/p>\n<ul>\n<li><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/pythonguides.com\/python-change-django-version\/\"  rel=\"noreferrer noopener\" class=\"rank-math-link\">Python Change Django Version<\/a><\/li>\n<li><a aria-label=\" (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/pythonguides.com\/python-django-vs-pyramid\/\"  rel=\"noreferrer noopener\">Python Django vs Pyramid<\/a><\/li>\n<li><a aria-label=\" (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/pythonguides.com\/get-url-parameters-in-django\/\"  rel=\"noreferrer noopener\">Get URL parameters in Django<\/a><\/li>\n<li><a aria-label=\" (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/pythonguides.com\/django-crud-example-with-postgresql\/\"  rel=\"noreferrer noopener\">Django CRUD example with PostgreSQL<\/a><\/li>\n<\/ul>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In this\u00a0Python Django Tutorial, I will show you how to perform union operation on models Django. You will understand how to perform union operations on querysets that belong to the same model. Also, you will learn how to perform union operations on query sets that belong to different models. Union operation on models Django So &#8230; <a title=\"Union operation on models Django\" class=\"read-more\" href=\"https:\/\/pythonguides.com\/union-operation-on-models-django\/\" aria-label=\"More on Union operation on models Django\">Read more&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":2034,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Union operation on models Django - ITTeacherITFreelance.hk","description":"In this\u00a0Python Django Tutorial, I will show you how to perform union operation on models Django. You will understand how to perform union operations on queryset"},"footnotes":""},"categories":[10700],"tags":[10773],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329331"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2034"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329331"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329331\/revisions"}],"predecessor-version":[{"id":329332,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329331\/revisions\/329332"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}