BUILD THE REST API USING PYTHON DJANGO - PART 3 🐍
Tags: javascript, django, programming, python
Published: 2021-02-21 13:07:39

Table Of Contents
        * 🤓[INTRO](#intro)
        * 🧠[MODELS](#models)
        * 🖖🏻[HOW DO WE TRANSLATE RELATIONSHIPS](#translate)
        * 💃🏻[MODELS](#models)
        * 👔[EMPLOYEE MODEL](#employee)
        * 🏢[SECTOR](#sector)
        * 📖[PROJECT](#project)
        * 💼[EXPOSE MODELS TO ADMIN PANEL](#admin)
        * 👨🏻‍💻[CODE](#code)
        * 🙏[THANK YOU](#thank-you)


## 🤓 INTRO <a name="intro"></a>
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.

{% link https://dev.to/codespresso/build-the-rest-api-using-python-django-part-2-2ln3 %}

Please feel free to connect with me via [Twitter](https://twitter.com/lazar_dev), [Instagram](https://www.instagram.com/codespressoweb/) or [LinkedIn](https://www.linkedin.com/in/lazar-stankovic-44a199177/)

Let's start! 🚀

![dance](https://media.giphy.com/media/U4DswrBiaz0p67ZweH/giphy.gif)


## 🧠 MODELS <a name"models"></a>
We start with an image from the previous post, our ER diagram.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7btoi0ylt64z7u11hg4.jpg)

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 <a name="translate"></a>

**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 <a name="employee"></a>
In the **models.py** we add the following code.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndl3q9t47e2nk6649eok.png)

**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
```
In your migrations folder you should see something like this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7hqrj4y940vi3gzdltn9.png)
### 🏢 SECTOR MODEL <a name="sector"></a>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0ra4wxy949jqzxb7nut.png)

## 📖 PROJECT MODEL <a name="project"></a>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5h2sunj6e06n9288jua.png)

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

## 💼 EXPOSE MODELS TO ADMIN PANEL <a name="admin"></a>
In our **admin.py** we add the following code

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/810lpaby1gwo92opjqe5.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0id5bunm1zmzajgsel43.png)

Try adding an employee, project, and sector. You should be able to add the data you want. 🚀 

## 👨🏻‍💻 CODE <a name="code"></a>

{% github https://github.com/Dwarf95/PythonDjangoTutorial %}

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!<a name="thank-you"></a>
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!
<a href='https://ko-fi.com/Z8Z33D1HJ' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi4.png?v=2' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>

Have a nice time hacking! 😊