Skip to content

Instantly share code, notes, and snippets.

View jlucasoliveira's full-sized avatar
🏠
Working from home

José Lucas jlucasoliveira

🏠
Working from home
View GitHub Profile
from __future__ import annotations
from typing import Type
from rest_framework import filters
from django.db.models.query import QuerySet
from django.http.request import HttpRequest
from django.views import View
@jlucasoliveira
jlucasoliveira / nested_serializer.py
Created September 15, 2021 12:17
Hidden field to restrieve parent object when use drf-nested-routers
from __future__ import annotations
from typing import Optional, Type
from django.db.models import Model
class NestedObject:
requires_context = True
@jlucasoliveira
jlucasoliveira / decorator.py
Last active July 19, 2021 16:50
Auto schema on viewset without override all methods
from typing import Callable, List, ClassVar, Union
from rest_framework.views import APIView
# Inspired on https://stackoverflow.com/q/64185352
def auto_schema_once(*, names: Union[List[str], str], decorator: Callable, schemas: dict) -> Callable:
if names == "__all__":
names = ["create", "list", "retrieve", "destroy", "update", "partial_update"]
def _decorator(cls: ClassVar[APIView]) -> ClassVar[APIView]: