Ini­tial­is­ing the data­base

OpenSource For You - - DEVELOPERS HOW TO -

The in­stalled apps are listed in the INSTALLED_APPS con­stant in set­


‘rest_frame­work’, ‘Task’, )

Any global set­tings for a REST frame­work API are kept in a sin­gle con­fig­u­ra­tion dic­tio­nary named REST_ FRAME­WORK.


‘DEFAULT_MODEL_SERIALIZER_CLASS’: ‘rest_frame­­ri­al­iz­ers.

ModelSe­ri­al­izer’, }

Cre­at­ing mod­els

Let’s go back to the Task app in the folder Task and cre­ate the mod­els we need in the file /Task/mod­ In or­der to de­fine a Task model, we need to de­rive from the Model class. Let’s use User class of the stan­dard au­then­ti­ca­tion as a for­eign key in the owner at­tribute.

We de­fine an owner, task name, de­scrip­tion of the task, a sta­tus and an at­tribute which stores the last time a model got up­dated.

from django.db im­port mod­els from django.con­trib.auth.mod­els im­port User class TaskModel(mod­els.Model): user = mod­els.OneToOneField(User) task_­name = mod­els.CharField(max_length=100) task_de­scrip­tion = mod­els.Tex­tField(max_length=200)

sta­tus = mod­els.BooleanField(de­fault=False) date = mod­els.DateTimeField(au­to_now_add=True) We have to set up the data­base for stor­ing data. In the de­fault set­tings, an SQLite data­base with the re­quired schema is au­to­mat­i­cally created with the fol­low­ing com­mands:

$ (env) python man­ makem­i­gra­tions $ (env) python man­ mi­grate

Cre­at­ing se­ri­alis­ers

We’ll de­clare a se­ri­aliser that we can use to se­ri­alise and de­se­ri­alise data that cor­re­sponds to TaskModel ob­jects.

Let’s cre­ate a new mod­ule named Task/se­ri­al­iz­ that we’ll use for our data rep­re­sen­ta­tions.

The ModelSe­ri­al­izer class pro­vides a short­cut that lets you au­to­mat­i­cally cre­ate a Se­ri­al­izer class with fields that cor­re­spond to the Model fields.

from django.con­trib.auth.mod­els im­port User from rest_frame­work im­port se­ri­al­iz­ers from .mod­els im­port TaskModel class TaskSe­ri­al­izer(se­ri­al­iz­ers.ModelSe­ri­al­izer):

user = se­ri­al­iz­ers.CharField(source=’user. user­name’,read­_only=True) class Meta: model = TaskModel fields = (‘user’,’task_­name’,’task_ de­scrip­tion’,’sta­tus’,’date’) class Regis­tra­tionSe­ri­al­izer (se­ri­al­iz­ers.ModelSe­ri­al­izer): pass­word = se­ri­al­iz­ers.CharField(write_only=True) def cre­ate(self, val­i­dat­ed_­data): user = User.ob­jects.cre­ate(

user­name = val­i­dat­ed_­data[‘user­name’]

) user.set_­pass­word(val­i­dat­ed_­data[‘pass­word’]) re­turn user class Meta: model = User fields = (‘user­name’,’pass­word’)

We de­fined two se­ri­aliser classes TaskSe­ri­al­izer and Regis­tra­tionSe­ri­al­izer. TaskSe­ri­al­izer has the user as a read­_only se­ri­aliser field, which ref­er­ences our logged in user or we can say a user who has per­mis­sion to per­form CRUD op­er­a­tions on the Task model.

Regis­tra­tionSe­ri­al­izer has a pass­word field as a write_only be­cause we don’t want to se­ri­alise the hashed pass­word. We over­ride the se­ri­alis­ers cre­ate() method in or­der to cre­ate a user in­stance.

Ad­just­ing the URL dis­patcher

Now, let’s set up the URL dis­patcher in the file /TaskAPI/ The URL dis­patcher sets the URL routes to spe­cific views in Django. A view han­dles logic and sends HTTP re­sponses.

The REST frame­work adds sup­port for au­to­matic URL rout­ing to Django, us­ing the Routers.Django REST frame­work, which pro­vides Sim­pleRouter, De­fault­Router and Cus­tomRouters.

Let’s use Sim­pleRouter and URL pat­terns in this pro­ject for now.

We will im­ple­ment TaskView for the cre­ation, list­ing, dele­tion and up­da­tion of tasks and Regis­tra­tionView for user reg­is­tra­tion.

from django.conf.urls im­port in­clude, url from django.con­trib im­port ad­min from rest_frame­work im­port routers from Task im­port views

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.