Django

  • OWASP - Django Security Cheat Sheet
  • Ninja
  • Scaffold
  • Avoid overwriting Model.delete. For example, overwriting to ensure soft delete (idea from django-tenant-users):
    def delete(self, *args, hard=False, **kwargs):
    	if not hard:
    		raise DeleteError("Use Model.soft_delete()")
    	super().delete(*args, **kwargs)ht
    
  • How to test unmanaged models? Source
    # conftest.py
    def pytest_sessionstart():
    	from django.apps import apps
    
    	unmanaged_models = [m for m in apps.get_models() if not m._meta.managed]
    
    	for m in unmanaged_models:
    		m._meta.managed = True
    
  • virtual_only fields
  • django.core.exceptions.ImproperlyConfigured: Cannot import '<app>'. Check that '<project>.<app>.apps.<App>Config.name' is correct. #troubleshooting
    • Rename <App>Config.name from <app> to <project>.<app>

Admin Link to heading

Auth Link to heading

GraphQL Server Link to heading

  • Ariadne
    • Missing maintainer
  • Graphene
    • docs
    • Poor integration between Models and Queries
  • Strawberry
    • Needs to define a schema class. Example:
    @strawberry.django.type(models.Fruit)
    class Fruit:
        id: auto
        name: auto
        color: 'Color'
      ```
    
  • Tartiflette

Health Check Link to heading

Request/Response Cycle Link to heading

flowchart TD

asgi["(A|W)SGI"]
db[Database]
client[Client]


subgraph Django
	middlewares[Middlewares]
	urls[URLs]
	view[View]
	orm[ORM]
	templates[Templates]

	middlewares-- Request -->urls-- Request -->view
	view-- Response -->middlewares

	middlewares -. Query objects ...- orm
	view-. Query objects ...- orm
	view-. Render ...- templates
end

client<-- HTTP req/res -->asgi
asgi<-- Request/Response -->middlewares

orm<-. Query/Data .->db

Servers Link to heading

ASGI Link to heading

  • Daphne - Django Channels HTTP/WebSocket server
  • Hypercorn -  ASGI and WSGI. Supports HTTP/1, HTTP/2, WebSockets (over HTTP/1 and HTTP/2). Can utilise asyncio, uvloop, or trio.
  • Mangum - running ASGI applications in AWS#Lambda.
  • uvicorn - supports HTTP/1.1 and WebSockets.

WSGI Link to heading

Testing Link to heading

  • (pytest) How to define a project conftest (shared between all apps)?
    • The conftest.py must be in the same directory of manage.py

Tree structures Link to heading

  • django-mptt - Utilities for implementing Modified Preorder Tree Traversal (This project is currently unmaintained)
  • django-tree - Fast and easy tree structures (In beta, it can’t be used yet in production.)
  • django-treebeard - Efficient tree implementations
  • django-tree-queries - Adjacency-list trees using recursive common table expressions