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

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.


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.

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


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.

- 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 |
|--------|--------|
$ from modelrelations.models import CarModel, FuelType$ c180 = CarModel.objects.create(name=”C180")
$ c200 = CarModel.objects.create(name="C200")
$ CarModel.objects.all()<QuerySet […


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.

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

- 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 |
|----------|--------------|
$ 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>
]>
$ 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…


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.

As you can see…


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.

https://flaviocopes.com/jwt/

In…

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