Django Models Many-To-Many Relationship

This is our eighteenth article in Django, in this article we are going to talk about Django Models Many-To-Many Relationship, so there are three types of Django Models Relationship, we have Many To Many Relationship, One To- Many Relationship and One-To-One Relationship. particularly in this article we are going to focus on Many To Many Relationship in Django Models. For defining Many To Many Relationship, we need to use ManyToManyField.

for example we have two models, so the first one is Article and the Second one is Publication. so an article can be published in multiple publication, and a Publication has multiple Article. 

 

 

 

Flask Web Development Tutorials

1: Flask CRUD Application with SQLAlchemy 

2: Flask Creating News Web Application 

3: Flask Creating REST API with Marshmallow 

 

 

 

Python GUI Development Tutorials

1: PyQt5 GUI Development Tutorials

2: Pyside2 GUI Development Tutorials

3: wxPython GUI Development Tutorials

4: Kivy GUI Development Tutorials 

5: TKinter GUI Development Tutorials

 

 

So now we need to create a New Project in Django, also for django installation and creating New Project you can read this article Django Introduction & Installation. but you can use this command for creating of the New Project.

 

 

 

 

After creating of the Django Project, you need to migrate your project. make sure that you have changed the directory to the created project.

 

 

 

So after doing this, let’s create our Django App, i have already talked about Django App in one of my articles, you can check this Django Apps & URL Routing.

 

 

 

After creation of the App, you need to add the created App in your settings.py file INSTALLED_APP section .

 

 

 

 

Also we need to create a super user, because we will use the django admin panel in this article, you can use this command for creating of the super user.

 

 

 

 

Now open your models.py in your MyApp App, and add your model, basically we are going to create two models, one for Publication and the second is for Article. and we want to use Many To Many Relationship with our these two models.

 

 

 

After creating of the models, we need to migrate our project, first you need to do migrations and after that migrate.

 

 

 

After doing that, you will see this output in the terminal.

 

 

 

Now you need to migrate.

 

 

 

And this will be the output in the terminal.

 

 

 

 

Because we are working with Django Admin Panel, so we need to add our these two models in the admin.py file, i have already talked about django admin panel in one of my articles, you can read this article Django Creating Super User.

 

 

 

 

OK now if you run your Project, and check admin panel we have our two models.

Django Admin Panel
Django Admin Panel

 

 

 

So now let’s just add some data, iam going to use Django Shell, you can use this command for opening Django Shell. make sure that you have changed your directory to your Django Project.

 

 

 

We are going to add some publications like this, first  i need to import my models, and after that i create my publications.

 

Now we have three Publication in our database.

 

 

 

Let’s just add an Article to our database.

 

 

 

 

Now if you check your Django Admin Panel, and if you open your first Article, you will see that for the first article we have three Publications, but there is no association between them.

Django Admin Panel
Django Admin Panel

 

 

 

For example in here we are going to create association or relationship between first Article and first Publication.

 

 

 

 

Now if you check your admin panel, you can see that we have a relationship between the first Article and the first Publication.

Django Many To Many Relationship
Django Many To Many Relationship

 

 

 

OK let’s add another Publication to our first article.

 

 

 

 

Now if you check your admin panel, you can see that we have two Publications for our one Article.

Django Models Many-To-Many Relationship
Django Models Many-To-Many Relationship

 

 

 

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×