Django models operate by default on relational database systems (RDBMS) and support relationships.

Image for post
Image for post

Django Follows the 3 model Relationships:

1- One-To-One Relationship
2- One-To-Many Relationship
3- Many-To-Many Relatiosnship

One-To-One Relationship (OneToOneField)

One record in a table is associated with one and only one record in another table.

- Car company can have only one CEO.
- CEO can work only one Car company.

Image for post
Image for post

For more information about one-to-one relationship, you can check :

One-To-Many Relationship (ForeignKey)

One record in a table can be associated with one or more records in another table.

- Car company can have one or more car model.
- Car model can only belong to one car company.

Image for post
Image for post

For more information about one-to-many relationship, you can check…


Django Model Relations — 4

Before learning about this, you may want to take a look at one-to-one, one -to-many and many-to-many relationships.

POLYMORPHIC ONE-TO-MANY RELATIONSHIP

Image for post
Image for post

- Sony can be sound system of many car models
- Pioneer can be sound system of many car models
- Car model can have only one sound system (Sony or Pioneer)

|-------------|---------------|
| Car Model | Sound System |
|-------------|---------------|
| C180 | S1 (SONY) |
| C200 | S1 (SONY) |
| X1 | S2 (SONY) |
| A3 | P1 (PIONEER) |
|-------------|---------------|

Add Sony:

$ from modelrelations.models import CarModel, Sony, Pioneer

$ sony1 = Sony.objects.create(name=”Sony Audio System”)
$ sony2 = Sony.objects.create(name=”Sony …

Django Model Relations — 3

Before learning about this, you may want to take a look at one-to-one and one -to-many relationships.

Many-To-Many Relationship

Multiple records in a table are associated with multiple records in another table.

Image for post
Image for post

- Many car models can run on same fuel type.
- Car model can run on diffrent fuel types.

|--------|--------|
|CarModel|FuelType|
|--------|--------|
| C180 | Diesel |
| C180 | Gas |
| C200 | Diesel |
| C200 | Hybrid |
| C220 | Gas |
|--------|--------|

Add CarModel

$ from modelrelations.models import CarModel, FuelType$ c180 = CarModel.objects.create(name=”C180")
$ c200 = CarModel.objects.create(name="C200")
$ CarModel.objects.all()<QuerySet [
<CarModel: C180>,
<CarModel: C200>…

Django Model Relations — 2

Before learning about this, you may want to take a look at one-to-one relationship.

One-To-Many Relationship

One record in a table can be associated with one or more records in another table.

Image for post
Image for post

- Car company can have one or more car model.
- Car model can only belong to one car company.

|--------|----------|
|CarModel|CarCompany|
|--------|----------|
| C180 | Mercedes |
| C200 | Mercedes |
| X1 | BMW |
| X3 | BMW |
| X5 | BMW |
| X6 | BMW |
|--------|----------|
$ from modelrelations.models import Car, CarModel$ mercedes = Car.objects.create(name="Mercedes")
$ bmw = Car.objects.create(name="BMW")
$…

Django Model Relations — 1

One record in a table is associated with one and only one record in another table.

Image for post
Image for post

- Car company can have only one CEO.
- CEO can work only one Car company.

|----------|--------------|
|CarCompany| CEO |
|----------|--------------|
|Mercedes | Adam Smith |
|BMW | Jack Ryan |
|Audi | Kevin Bishop |
|----------|--------------|

Add Car:

$ from modelrelations.models import Car, Ceo$ mercedes = Car.objects.create(name=”Mercedes”)
$ audi = Car.objects.create(name=”Audi”)
$ bmw = Car.objects.create(name=”BMW”)
$ Car.objects.all()<QuerySet [
<Car: Mercedes>,
<Car: Audi>,
<Car: BMW>
]>

Add Ceo:

$ mercedes_ceo = Ceo.objects.create(name=”Adam Smith”, car=mercedes)
$ audi_ceo = Ceo.objects.create(name=”Kevin Bishop”, car=audi)
$ bmw_ceo = Ceo.objects.create(name=”Jack …

Build a Product Review Backend with DRF — Part 10

When using JWT authentication, the client side stores the token and attaches it to every request. So, the first thing to do when logging out, is just delete the token you stored on local storage. In that case the client won’t have a token to put in the request, thus causing unauthorized response status. But this is not enough. the token still exists somewhere and it is still valid. It’s not that simple with JWT. It is not possible to forcefully delete or invalidate an existing token. The tokens can be expired but you can’t do it on demand.

JWT should be stateless, you should store everything in the payload and skip performing a DB query on every request. But you might need to some queries for logout functionality. If you really must have log out functionality, then you can use a black list. You still have to lookup the token on every request to be sure it is still valid. …


Build a Product Review Backend with DRF — Part 9

We recently wrote an article about JWT Authentication and User Registration. We’ll add change password and update profile functionality with this article.

For change password open auth/serializers.py and type the following lines of code:

Password fields must be same. We can validate these fields with serializers validate() method. We check the user password with validate_old_password() method. Finally we save new password with update() method.

We’re ready to create the view. Open auth/views.py and create a ChangePasswordView with a update action.

Open auth/urls.py and add change password endpoint. UpdateAPIView used for update-only endpoints for a single model instance. …


Build a Product Review Backend with DRF — Part 8

We recently wrote an article about JWT Authentication. Now we can create new app for user management. For creating new app run startapp command.

python manage.py startapp auth

Actually, we have created endpoints for login before. At this stage we will move them to the auth application. For this;

Create auth/serializers.py move MyTokenObtainPairSerializer from reviews/serializers.py.

Move MyObtainTokenPairView to auth/views.py from reviews/serializers.py.

Open medium/urls.py and change code with:

Open auth/urls.py and type the following lines of code:

Login endpoint is ready. we should send a POST request to API for checking login endpoint.

Image for post
Image for post

As you can see in the picture above the login works very well and the access token is returning. …


Build a Product Review Backend with DRF — Part 7

What is JWT?

JSON Web Token (JWT) is an open standard that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWT used to create access tokens for an application. JWT is good for API authentication, and server-to-server authorization.

The server generates a token that certifies the user identity, and sends it to the client. The client will send the token back to the server for every subsequent request, so the server knows the request comes from a particular identity.

Image for post
Image for post
https://flaviocopes.com/jwt/

In its compact form, JSON Web Tokens consist of three parts separated by dots (.), which are header, payload and signature.


Build a Product Review Backend with DRF — Part 6

What is Cors?

Cross Origin Resource Sharing (CORS) is a security mechanism that allows a web page from one domain or origin to access a resource with a different domain. Server knows where a request is coming from and can choose whether or not to accept the request based on this.

Image for post
Image for post
https://drawings.jvns.ca/cors/

We can use test-cors.org for testing CORS requests.

Image for post
Image for post
https://www.test-cors.org

Enter your api url in “Remote URL” and submit request. When CORS not enabled, the result will look like the following

Image for post
Image for post
https://www.test-cors.org

We’ll use dango-cors-headers package for enabling cors.

A Django App that adds Cross-Origin Resource Sharing (CORS) headers to responses. …

About

Yunus Emre Cevik

?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store