Taking you API to the next level Django Rest Framework $whoami - - PowerPoint PPT Presentation

taking you api to the next level django rest framework
SMART_READER_LITE
LIVE PREVIEW

Taking you API to the next level Django Rest Framework $whoami - - PowerPoint PPT Presentation

Taking you API to the next level Django Rest Framework $whoami Carlos Martnez Backend Developer en twitter @carlosmart626 github carlosmart626 https://carlosmart.co $whoami Colombia @carlosmart626 $whoami @carlosmart626 5


slide-1
SLIDE 1

Taking you API to the next level

slide-2
SLIDE 2

Django Rest Framework

slide-3
SLIDE 3

Carlos Martínez 

Backend Developer en twitter @carlosmart626 github carlosmart626 https://carlosmart.co

$whoami

slide-4
SLIDE 4

$whoami

Colombia 

@carlosmart626

slide-5
SLIDE 5

$whoami

@carlosmart626

slide-6
SLIDE 6

5 Minute API

slide-7
SLIDE 7

Ticket Store

Users Events Promotors Tickets

@carlosmart626

slide-8
SLIDE 8

Display different data based on context Getting better performance Filtering information Permissions How to render results in different formats?

@carlosmart626

slide-9
SLIDE 9

Reuse Serializers

slide-10
SLIDE 10

How to reuse your serializers to show different representations?

Create multiple ModelSerializer with different selected fields

@carlosmart626

slide-11
SLIDE 11

How to reuse your serializers to show different representations?

DynamicSerializers!

@carlosmart626

slide-12
SLIDE 12

Dynamic Serializers

Use DynamicSerializer instead ModelSerializer

@carlosmart626

slide-13
SLIDE 13

Dynamic Serializers

Use DynamicSerializer instead ModelSerializer

@carlosmart626

slide-14
SLIDE 14

Dynamic Serializers

@carlosmart626

slide-15
SLIDE 15

Dynamic Serializers

Now you can reuse the serializer easier

@carlosmart626

slide-16
SLIDE 16

Getting better performance

@carlosmart626

slide-17
SLIDE 17

Prefetch

Keep in mind the number of queries

@carlosmart626

slide-18
SLIDE 18

Prefetch

Select Related

Follow foreign key relationships by getting a single but more complex query

@carlosmart626

slide-19
SLIDE 19

Prefetch

django/querysets/#select-related

@carlosmart626

Select Related

Follow foreign key relationships by getting a single but more complex query

slide-20
SLIDE 20

Prefetch

@carlosmart626

Select Related

Follow foreign key relationships by getting a single but more complex query

slide-21
SLIDE 21

Prefetch

Query

@carlosmart626

slide-22
SLIDE 22

Prefetch

Query

@carlosmart626

slide-23
SLIDE 23

Prefetch

Prefetch Related

Great to many-to-many and many-to-one

@carlosmart626

slide-24
SLIDE 24

Prefetch

@carlosmart626

Prefetch Related

Similar to ‘select_related’ creates a more queries executed to get the related data

slide-25
SLIDE 25

Prefetch

@carlosmart626

Prefetch Related

Similar to ‘select_related’ creates a more queries executed to get the related data

slide-26
SLIDE 26

Prefetch

Prefetch Object

This object can be used to customize the ‘prefetch_related’ operations

django/querysets/#prefetch-objects

@carlosmart626

slide-27
SLIDE 27

Prefetch

Prefetch Object

This object can be used to customize the ‘prefetch_related’ operations

django/querysets/#prefetch-objects

@carlosmart626

slide-28
SLIDE 28

Filtering

slide-29
SLIDE 29

django-url-filter

Let’s filter your endpoints in a better way

@carlosmart626

slide-30
SLIDE 30

Add filter_backends and select your filter fields

django-url-filter

@carlosmart626

slide-31
SLIDE 31

Add filter_backends and select your filter fields

django-url-filter

@carlosmart626

slide-32
SLIDE 32

django-url-filter

@carlosmart626

slide-33
SLIDE 33

ModelFilterSet

django-url-filter

@carlosmart626

slide-34
SLIDE 34

Cache

@carlosmart626

slide-35
SLIDE 35

Django Cache in DRF ModelViewSets

View example

Cache

@carlosmart626

slide-36
SLIDE 36

Django Cache in DRF ModelViewSets

View example

Cache

@carlosmart626

slide-37
SLIDE 37

Django Cache in DRF ModelViewSets

View example

Cache

@carlosmart626

slide-38
SLIDE 38

DON’T FORGET TO INVALIDATE CACHE

Key invalidation

Cache

@carlosmart626

slide-39
SLIDE 39

django-cacheops

Cacheops let’s you cache the ORM

@carlosmart626

slide-40
SLIDE 40

django-cacheops

Cacheops can work using redis

@carlosmart626

slide-41
SLIDE 41

django-cacheops

Cacheops common configurations

Default time out Apps/Models to cache with operations and time settings

@carlosmart626

slide-42
SLIDE 42

django-cacheops

Cacheops common configurations

Default time out Apps/Models to cache with operations and time settings

@carlosmart626

slide-43
SLIDE 43

django-cacheops

Cacheops provides different ways to cache ORM operations

It’s automatic! But, you can define a queryset and append cache() at the end to cache the ORM results.

https://github.com/Suor/django-cacheops

@carlosmart626

slide-44
SLIDE 44

django-cacheops

Cacheops provides different ways to cache ORM operations

It’s automatic! But, you can define a queryset and append cache() at the end to cache the ORM results.

https://github.com/Suor/django-cacheops

@carlosmart626

slide-45
SLIDE 45

Permissions

slide-46
SLIDE 46

Permissions

DRYPermissions

You can define global,

  • bject and action

permissions in each one

  • f your models.

@carlosmart626

slide-47
SLIDE 47

DRYPermissions

@carlosmart626

slide-48
SLIDE 48

DRYPermissions

Using DRYPermission in a ViewSet

Add to permission_classes DRYPermissions

@carlosmart626

slide-49
SLIDE 49

DRYPermissions

Using DRYPermission in a ViewSet

Add to permission_classes DRYPermissions

@carlosmart626

slide-50
SLIDE 50

DRYPermissions

Global Permissions

You have access to the request so you can get information about the user

@carlosmart626

slide-51
SLIDE 51

DRYPermissions

Object Permissions

You can define object permissions and you get both request and the object to make validations

@carlosmart626

slide-52
SLIDE 52

DRYPermissions

Field/Action Permissions

You can define logic to access to fields and actions within a model viewset

@carlosmart626

slide-53
SLIDE 53

DRYPermissions

Custom permission class

@carlosmart626

slide-54
SLIDE 54

DRYPermissions

Custom permission class

@carlosmart626

slide-55
SLIDE 55

DRYPermissions

Custom permission class

@carlosmart626

slide-56
SLIDE 56

DRYPermissions

DRYPermission on APIView

Outside a Viewset you need to add to the serializer a context

https://github.com/dbkaplan/dry-rest-permissions

@carlosmart626

slide-57
SLIDE 57

DRYPermissions

DRYPermission on APIView

Outside a Viewset you need to add to the serializer a context

https://github.com/dbkaplan/dry-rest-permissions

@carlosmart626

slide-58
SLIDE 58

Rederers

slide-59
SLIDE 59

XLSXRenderer

Drf-renderer-xlsx

This package let you export excel files directly from your api instead to get a son you can get a xlsx file with all the filters you already define at the URL

@carlosmart626

slide-60
SLIDE 60

XLSXRenderer

It’s very simple to use

@carlosmart626

slide-61
SLIDE 61

XLSXRenderer

It’s very simple to use

@carlosmart626

slide-62
SLIDE 62

Code

https://github.com/CarlosMart626/ djangocon-2019

slide-63
SLIDE 63

Demo 爛

slide-64
SLIDE 64
slide-65
SLIDE 65

Thank you

@carlosmart626