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 - - 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
Django Rest Framework
Carlos Martínez
Backend Developer en twitter @carlosmart626 github carlosmart626 https://carlosmart.co
$whoami
$whoami
Colombia
@carlosmart626
$whoami
@carlosmart626
5 Minute API
Ticket Store
Users Events Promotors Tickets
@carlosmart626
Display different data based on context Getting better performance Filtering information Permissions How to render results in different formats?
@carlosmart626
Reuse Serializers
How to reuse your serializers to show different representations?
Create multiple ModelSerializer with different selected fields
@carlosmart626
How to reuse your serializers to show different representations?
DynamicSerializers!
@carlosmart626
Dynamic Serializers
Use DynamicSerializer instead ModelSerializer
@carlosmart626
Dynamic Serializers
Use DynamicSerializer instead ModelSerializer
@carlosmart626
Dynamic Serializers
@carlosmart626
Dynamic Serializers
Now you can reuse the serializer easier
@carlosmart626
Getting better performance
@carlosmart626
Prefetch
Keep in mind the number of queries
@carlosmart626
Prefetch
Select Related
Follow foreign key relationships by getting a single but more complex query
@carlosmart626
Prefetch
django/querysets/#select-related
@carlosmart626
Select Related
Follow foreign key relationships by getting a single but more complex query
Prefetch
@carlosmart626
Select Related
Follow foreign key relationships by getting a single but more complex query
Prefetch
Query
@carlosmart626
Prefetch
Query
@carlosmart626
Prefetch
Prefetch Related
Great to many-to-many and many-to-one
@carlosmart626
Prefetch
@carlosmart626
Prefetch Related
Similar to ‘select_related’ creates a more queries executed to get the related data
Prefetch
@carlosmart626
Prefetch Related
Similar to ‘select_related’ creates a more queries executed to get the related data
Prefetch
Prefetch Object
This object can be used to customize the ‘prefetch_related’ operations
django/querysets/#prefetch-objects
@carlosmart626
Prefetch
Prefetch Object
This object can be used to customize the ‘prefetch_related’ operations
django/querysets/#prefetch-objects
@carlosmart626
Filtering
django-url-filter
Let’s filter your endpoints in a better way
@carlosmart626
Add filter_backends and select your filter fields
django-url-filter
@carlosmart626
Add filter_backends and select your filter fields
django-url-filter
@carlosmart626
django-url-filter
@carlosmart626
ModelFilterSet
django-url-filter
@carlosmart626
Cache
@carlosmart626
Django Cache in DRF ModelViewSets
View example
Cache
@carlosmart626
Django Cache in DRF ModelViewSets
View example
Cache
@carlosmart626
Django Cache in DRF ModelViewSets
View example
Cache
@carlosmart626
DON’T FORGET TO INVALIDATE CACHE
Key invalidation
Cache
@carlosmart626
django-cacheops
Cacheops let’s you cache the ORM
@carlosmart626
django-cacheops
Cacheops can work using redis
@carlosmart626
django-cacheops
Cacheops common configurations
Default time out Apps/Models to cache with operations and time settings
@carlosmart626
django-cacheops
Cacheops common configurations
Default time out Apps/Models to cache with operations and time settings
@carlosmart626
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
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
Permissions
Permissions
DRYPermissions
You can define global,
- bject and action
permissions in each one
- f your models.
@carlosmart626
DRYPermissions
@carlosmart626
DRYPermissions
Using DRYPermission in a ViewSet
Add to permission_classes DRYPermissions
@carlosmart626
DRYPermissions
Using DRYPermission in a ViewSet
Add to permission_classes DRYPermissions
@carlosmart626
DRYPermissions
Global Permissions
You have access to the request so you can get information about the user
@carlosmart626
DRYPermissions
Object Permissions
You can define object permissions and you get both request and the object to make validations
@carlosmart626
DRYPermissions
Field/Action Permissions
You can define logic to access to fields and actions within a model viewset
@carlosmart626
DRYPermissions
Custom permission class
@carlosmart626
DRYPermissions
Custom permission class
@carlosmart626
DRYPermissions
Custom permission class
@carlosmart626
DRYPermissions
DRYPermission on APIView
Outside a Viewset you need to add to the serializer a context
https://github.com/dbkaplan/dry-rest-permissions
@carlosmart626
DRYPermissions
DRYPermission on APIView
Outside a Viewset you need to add to the serializer a context
https://github.com/dbkaplan/dry-rest-permissions
@carlosmart626
Rederers
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
XLSXRenderer
It’s very simple to use
@carlosmart626
XLSXRenderer
It’s very simple to use
@carlosmart626
Code
https://github.com/CarlosMart626/ djangocon-2019
Demo 爛
Thank you
@carlosmart626