Django-RestFramework Flashcards

1
Q

bjhjbHow to add djangorestframework to the project?

A

1.Install the project.
python -m pip install djangorestframework~=3.13.0
2.Add it to the django project.insettings.py in main project dir.append to the INSTALLED_APPS list the following line
‘rest_framework’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is it proffesional to keep the api urls within the same app or do we need to create a new app for that??

A

Both are fine.When kept within the same app usually as prefix like /api/ is added to differentiate it from the rest.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to set configurations for the rest_framework?

A

In settings file create a dict call REST_FRAMEWORK.Configurations are set here.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to dearl with cors issues in django?

A

1.Instal django-cors-headers
2.Insert the following entry in middleware list in settings.py just above commonmiddleware.
‘corsheaders.middleware.CoresMiddleware’
3.add a new list called CORS_ALLOWED_ORIGINS in settings.py file
add the hostname urls that are allowed to be accessed from the client side.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Ho to create a new user structure in dkango?

A

define a new app for this let it be accounts
create a customuser as shown below
———————————————————–
from django.contrib.auth.models import AbstractUser
class CustonUser(AbstractUser):

———————————————————–
Make sure you set AUTH_USER_MODEL IN SETTINGS to ‘acoounts.CustomUser’ #Why no model in b/w ???

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the 4 project level permissions available in restframework??

A

AllowAny,IsAuthenticated,IsAdminUser,IsAuthenticatedOrReadOnly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how to import permissions?

A

from rest_framework import permissions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to add a particular permission to a class view?

A

include a field called “permission_classes” and assign a list of possinble permissions like string in a tuple

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Is permission IsAuthenticated inherit from any base class .If yes name it?

A

BasePermission

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the 4 basic types of auth supported by rest_framework out of the box?

A

Basic auth,Session Auth,Token Auth,Default AUth(comb og basic and session auth)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Hoe does basic auth in rest_framework work?

A

base64 encoded usrname and password in a string is sent with every req.in authorization header
Disadvantages
Very insecure.HTTPS is a must
Inefficient username and password has to be checked every time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does Session Auth work?

A

username and pass sent with 1st request.a sesion obj is created and stored in server.session id for the same is sent back to th user.User store this on client side and sent with every request afterwards for auth.password is availbale till the end of the session.
Statefull
Adv
more secure as pass sent only once
more efficient since usename and pass has to be checked onl once
Disdv
session id must be kept upto date in all the servers.challenging for large sites.
session id onl valid in only 1 browser.do not work across multiple domain,
cookie sent out with every req event he one that do not require it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does Token Auth in rest_framework work?

A

Uniques token is generated and stored on the client side during first login.this token can be used in multiple apps.
Server does not keep the token .It is just checked to be valid or not.
Adv
Scaling servers is easier as session id do not have to kept uptodate.
token can be shared by multiple frontends
Disv
TOkens can get quite large as it contains a lot of information about the user.It can cause performance degradation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

rest_framework default auth setting?

A

A comb of session auth and basic auth.session id generated is stored in authorization header.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

import SessionAuthentication class.also name the rest_framework key for setting auth classes supported.

A
  1. rest_framework.authentication.SessionAuthentication
    2.DEFAULT_AUTHENTICATION_CLASSES
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

rest_framework key for persmission classes in settings.py

A

1.DEFAULT_PERMISSION_CLASSES

17
Q

Mention a 3rd party app to get inbuilt login,logout and password reset views?
mention how to install it .how to add it to settings.How to include urls from this app.Also mention the defult paths used by this application?

A

installation: pip install dj-rest-auth
appname to incl in installed apps: dj_rest_auth
urls: include(dj_rest_auth.urls)
default paths:
1. login/
2.logout/
3.password/reset. _> takes just the email
4.password/reset/confirm. -> takes new pass confirm pass,token,uid

18
Q

how to import viewsets?

A

from rest_framework import viewsets
ex viewset -< c=viewsets.ModelViewSet

19
Q

How to import Routers?

A

from rest_framework import routers
2 main routers=-> routers.SimpleRouter,router.DefaultRouter

20
Q

how to set up a path using router?

A

create a router

router=SimpleRouter()
#register a path
router.register(“pathname”,ViewSetObject,basename=”base-name”)
urlpatterns=router.urls