DEV Community

loading...
Cover image for BUILD THE REST API USING PYTHON DJANGO - PART 3 🐍

BUILD THE REST API USING PYTHON DJANGO - PART 3 🐍

codespresso profile image Codespresso ・4 min read

Table Of Contents
* πŸ€“INTRO
* 🧠MODELS
* πŸ––πŸ»HOW DO WE TRANSLATE RELATIONSHIPS
* πŸ’ƒπŸ»MODELS
* πŸ‘”EMPLOYEE MODEL
* 🏒SECTOR
* πŸ“–PROJECT
* πŸ’ΌEXPOSE MODELS TO ADMIN PANEL
* πŸ‘¨πŸ»β€πŸ’»CODE
* πŸ™THANK YOU

πŸ€“ INTRO

Hello, hackers! I hope you are all having a great weekend, it is a beautiful Sunday, and let's complete this week with another part of the "Building the REST API using Python Django" series.

If you haven't read the previous chapter, it's about setting up the environment and the project.

Please feel free to connect with me via Twitter, Instagram or LinkedIn

Let's start! πŸš€

dance

🧠 MODELS

We start with an image from the previous post, our ER diagram.

Alt Text

Each entity represented with the rectangular shape is a model, a class that has attributes that are represented by the oval shape.

Relationships are represented by the rhomboid shapes and are crucial for determining how the models are related.

πŸ––πŸ» HOW DO WE TRANSLATE RELATIONSHIPS

ONE-ON-ONE RELATIONSHIPS - For any binary type of relationships let's call it R(1:1) in the ER scheme, we identify the relations S and T that correspond to the entity types participating in the relationship R. We choose one of two relations, let's say relation S, and we include a foreign key that represents a corresponding primary key of the relation T.

ONE-ON-MANY RELATIONSHIPS - For any binary type of relationship let's call it R(1:N) in the ER scheme, we identify relation S that participates on the N side of the relationship. In the S we include the foreign key that corresponds to the primary key of the relation T.

MANY-TO-MANY RELATIONSHIPS - For any binary type of relationship let's call it R(M:N) in the ER scheme, we create a totally new relation P, so-called pivot table. The P includes the foreign keys corresponding to the primary keys of the relations that represent entity types participating in the relationship R.

We have two relationships that correspond to ONE-TO-MANY relationships and only one relationship corresponding to MANY-TO-MANY relationships.

This means that in the EMPLOYEE-SECTOR relationship the corresponding entity type on the N side of the relationship is Employee, which means, it will hold a foreign key to the Sector entity type.

In the SECTOR-PROJECT relationship, the corresponding entity type on the N side of the relationship is the Project entity type, which means, it will hold a foreign key to the Sector entity type.

In the EMPLOYEE-PROJECT relationship, we have a MANY-TO-MANY relationship. This means, that a new table, a pivot table, will be created with the foreign keys corresponding to the primary keys of the Employee and the Project entity type, respectively.

πŸ‘” EMPLOYEE MODEL

In the models.py we add the following code.

Alt Text

employee_id - is a unique identifier represented by the 128-bit number, it's a self-generated, non-editable field.
first_name, middle_name, last_name - are character fields with specified maximum length and are required
gender - is a character field that is representing the choices for the gender specified by GENDER_CHOICES
address - is a character field representing an address
salary - is a decimal field, with two decimal points and is required

Now let's make our first migrations:

python manage.py makemigrations
python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

In your migrations folder you should see something like this:
Alt Text

🏒 SECTOR MODEL

Alt Text

πŸ“– PROJECT MODEL

Alt Text

Execute the code for creating another migration and migrate the models.

πŸ’Ό EXPOSE MODELS TO ADMIN PANEL

In our admin.py we add the following code

Alt Text

Let's run our server, open it up in the browser, log in, redirect to the admin page HTTP://127.0.0.1:8000/admin and check if our models are exposed.

In your browser you should see this:
Alt Text

Try adding an employee, project, and sector. You should be able to add the data you want. πŸš€

πŸ‘¨πŸ»β€πŸ’» CODE

GitHub logo Dwarf95 / PythonDjangoTutorial

This is an official repository of the Codespresso Dev.to tutorial, regarding the "Building the REST API using Python Django" series.

PythonDjangoTutorial

This is an official repository of the Codespresso Dev.to tutorial, regarding the "Building the REST API using Python Django" series.

HOW TO RUN PROJECT

  • Setup you PostgreSQL database (name: company)
  • Edit settings.py and specify DATABASES name to company
  • Pull the code from this repository
  • Open it up in the PyCharm
  • Open terminal and execute pip install -r requirements.txt
  • Run python manage.py migrate
  • Run python manage.py runserver

That's it for now, I hope you enjoyed it. In the next chapter, we will set up a new url.py file for an API and write our views. Stay tuned! πŸ‘½

πŸ™ THANK YOU FOR READING!

References:
School notes...
School books...

Please leave a comment, tell me about you, about your work, comment your thoughts, connect with me!

β˜• SUPPORT ME AND KEEP ME FOCUSED!
Buy Me a Coffee at ko-fi.com

Have a nice time hacking! 😊

Discussion (6)

pic
Editor guide
Collapse
gerlc profile image
GerLC • Edited

Hello, I got a question with the ManyToManyField, how does it works? What happends if i put the ManyToManyField in the Employee model instead of the project?
In a mini project that i did, i created a model that contained the two IDs using the ForeignKey.
Thanks for the tutorial

Collapse
codespresso profile image
Codespresso Author

When you have the ManyToManyField, in the background, Django creates an intermediary join table to represent that ManyToMany relationship. You can read about it in the docs, there are well-explained ways of using the ManyToMany Field. Check it out! docs.djangoproject.com/en/3.1/ref/...

Collapse
bartosz profile image
Bartosz WΓ³jcik

How about posting source codes in text and not in that fancy carbon screenshots? You know, so ppl can actually copy it and use it?

Collapse
codespresso profile image
Codespresso Author • Edited

Yep, will do. Thank you for commenting. I'll create a repo and update this article. πŸ‘¨β€πŸ’»πŸš€

Collapse
rahmadsandy profile image
Rahmad Sandy

i cant wait to see next chapter,
awesome tutorial

Collapse
codespresso profile image
Codespresso Author

Thank you, Rahmad. Stay tuned, a new chapter coming up today!