Django Basics – Part2

In this post we will mainly see about django model and database operations

Step1

Reference  
https://docs.djangoproject.com/en/2.2/topics/db/models/

Let’s first added some fields to model so we can explore different type of fields our model will look like this

todo
========
id
task -- varchar 255
description - text
due_date - date
is_complete - boolean
complete_percentage - decimal
created_at - datetime
updated_at - datetime

Here is how the code looks for this
https://github.com/pythexcel/django_tutorial/commit/004d8f730c1f2c7cd8d0ae1c631251c7108e1c24

Also i got error when creating migrations to specify default values as well. Make sure run migrate commands to create the relevant database changes

Step2

Let’s look into relationship with models. e.g let create new model called “users” and associated “users” with “todo”

https://github.com/pythexcel/django_tutorial/commit/c3e3ae96dfb23042443d1460ac8fdcc15f1c7c8d

here update this code in Todo model when creating a relation between user model and todo model
> user = models.ForeignKey(User, on_delete=models.CASCADE,   db_constraint=False)

Step3

Let’s add route for login, registration as well and also add the relevant code to save user

For now i have added empty routes and this is how the code looks

https://github.com/pythexcel/django_tutorial/commit/5522af6cf340c54ed0563b727ff657ecc44b2b3a

There is a problem with this, which i see. Our routes for “login”, ” register” are like /todo/login and /todo/register which doesn’t look good. Rather it should be like “/auth/login” or “/auth/register” for this we need install another “app” called “user” which will do.

Now our code base looks like this
https://github.com/pythexcel/django_tutorial/commit/b4d0df869e78d0e946f83dd0287f1f395c5f7972

This is much better, the router is /user/login which makes better sense.

Also after moving everything to “user” app including views, models the code looks like this

https://github.com/pythexcel/django_tutorial/commit/9563fb5fa6e22871aaef418d3f71

There is one important thing in this about dependencies and migrations. Since we have two different apps “user” and “todo” during the migration process we need to mention that “todo” depends on user only then the foreign key can be setup. Read more about it here and its important
https://docs.djangoproject.com/en/2.1/topics/migrations/#dependencies

Code for login route as well
https://github.com/pythexcel/django_tutorial/commit/eea918bf6818992a486496ad6f09f74c24be247d

make sure to go through this route in detail and understand it. there are many new things in the codebase

Step4

Next, let’s update our existing to add todo. Since we need to assign to a user now.

But before going further

Right now we are seeing lot of problem with django and rest api to list a few

  1. Django model returns data which is not possible to send back as an json response
  2. There is no default to send back a json response
  3. All routes by default are protected via CSRF which doesn’t make sense for rest api’s

overall we can see that django by default is not suitable for rest api’s but rather very much suited for making a website.

But, based on current tech trends frameworks like angularjs, react, vue are more preferred for frontend and node/python are mainly used a rest api framework.

Hence we need a better framework to manage rest api for that we have
https://www.django-rest-framework.org/

Before going ahead we should switch to rest api framework.

excellence-social-linkdin
excellence-social-facebook
excellence-social-instagram
excellence-social-skype