TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Django REST – Typed Views

24 pointsby rsinger87over 5 years ago

5 comments

rsinger87over 5 years ago
I wanted wanted to share a package I made to enable use of type annotations for automatic validation&#x2F;sanitization in Django REST Framework.<p>I&#x27;ve borrowed ideas from the original API Star (<a href="https:&#x2F;&#x2F;github.com&#x2F;encode&#x2F;apistar" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;encode&#x2F;apistar</a>) (before it was an OpenAPI toolkit) and FastAPI (<a href="https:&#x2F;&#x2F;fastapi.tiangolo.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;fastapi.tiangolo.com&#x2F;</a>). Instagram engineering also had a recent blog post about how they do something similar with Django views (<a href="https:&#x2F;&#x2F;instagram-engineering.com&#x2F;types-for-python-http-apis-an-instagram-story-d3c3a207fdb7" rel="nofollow">https:&#x2F;&#x2F;instagram-engineering.com&#x2F;types-for-python-http-apis...</a>).
leowoo91over 5 years ago
Looks nice! That might save 4-5 lines in beginning of the API view functions for type checks and redundant 400 returns. Title should have &quot;Show HN&quot; I think.
评论 #21269476 未加载
tonyover 5 years ago
Starred.<p>Aside: If this is appealing to you (automated typing in REST) I think you would love GraphQL.<p>Typing is baked into graphql (<a href="https:&#x2F;&#x2F;graphql.org&#x2F;learn&#x2F;schema&#x2F;#type-system" rel="nofollow">https:&#x2F;&#x2F;graphql.org&#x2F;learn&#x2F;schema&#x2F;#type-system</a>). You can do it with graphene (<a href="https:&#x2F;&#x2F;graphene-python.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;graphene-python.org&#x2F;</a>) and django&#x27;s graphene-djanago (<a href="https:&#x2F;&#x2F;github.com&#x2F;graphql-python&#x2F;graphene-django" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;graphql-python&#x2F;graphene-django</a>) which handles ORM wrapping and a view baseclass.<p>Having experience with it, one major downside is how exception handling works in graphene. There is a strange implementation of promises which doesn&#x27;t translate well into python IMO. It ends up hijacking error resolution, making it hard to control execution.<p>On the other hand, being able to get instant documentation from graphql is a big benefit. Everything generates nicely to a schema.graphql file.<p>Instead of POST and PUT, graphql has a concept of mutations: <a href="https:&#x2F;&#x2F;graphql.org&#x2F;learn&#x2F;queries&#x2F;" rel="nofollow">https:&#x2F;&#x2F;graphql.org&#x2F;learn&#x2F;queries&#x2F;</a>, GET would be a query.<p>Also, graphql has built-in pagination via Connections: <a href="https:&#x2F;&#x2F;graphql.org&#x2F;learn&#x2F;pagination&#x2F;" rel="nofollow">https:&#x2F;&#x2F;graphql.org&#x2F;learn&#x2F;pagination&#x2F;</a><p>I still use REST in many places, but graphql has been one thing where the learning curve paid off. I wish REST had typing baked into it from the beginning.
评论 #21277408 未加载
ptwtover 5 years ago
Shameless self plug: a couple of days ago I posted a project that does pretty much the same thing but built on top of flask instead of django: <a href="https:&#x2F;&#x2F;github.com&#x2F;plainas&#x2F;flask-swagger-types" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;plainas&#x2F;flask-swagger-types</a><p>Also relying on Marshmallow but with the extra bonus of generating an openapi spec for you and hosting swagger ui.<p>It puzzles me that such approach is not the norm. Interesting to see more people tackling the same problem.
评论 #21283576 未加载
OJFordover 5 years ago
Nice. We do something similar with Django (but not DRF) + Marshmallow (+ marshmallow-dataclass (+ marshmallow-dataclass-djangofield which is a very small module I wrote)).