Django Follows the 3 model Relationships:
1- One-To-One Relationship
2- One-To-Many Relationship
3- Many-To-Many Relatiosnship
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.
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”)…
Before learning about this, you may want to take a look at one-to-one and one -to-many relationships.
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 [ <CarModel: C180>, <CarModel…
Before learning about this, you may want to take a look at one-to-one 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")$…
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 …
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…
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. …
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…
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.
In…
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.
We can use test-cors.org for testing CORS requests.
Django & Django Rest Framework