devlazar

Posted on

# BUILD THE REST API USING PYTHON DJANGO - PART 3 π

* π€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.

Let's start! π

## π§  MODELS

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

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.

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
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:

## π PROJECT MODEL

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

## πΌ EXPOSE MODELS TO ADMIN PANEL

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:

Try adding an employee, project, and sector. You should be able to add the data you want. π

# 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...

β SUPPORT ME AND KEEP ME FOCUSED!

Have a nice time hacking! π

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

devlazar

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/...

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?

devlazar • Edited

Yep, will do. Thank you for commenting. I'll create a repo and update this article. π¨βπ»π