Skip to main content

One-line CRUD registration for Django REST Framework. Built on top of DRF's mixins/generics so you never have to write a Serializer, ViewSet, or router.register() by hand for the common case.

Project description

humanoid-crud

A one-line CRUD registrar for Django REST Framework. Built on top of DRF's mixins/generics (it generates real ModelSerializer and ModelViewSet classes for you) — the point isn't to avoid mixins, it's that you never have to write or stack them by hand.

Install

pip install humanoid-crud

If you want filterset_fields (exact-match filtering), install the extra:

pip install humanoid-crud[filters]

Note: the package name on PyPI is humanoid-crud (hyphen, matching PyPI convention), but you import it in Python as humanoid_crud (underscore, since hyphens aren't valid in Python identifiers). This is normal — the same is true for packages like django-rest-frameworkimport rest_framework.

The problem this solves

The normal DRF CRUD recipe for one model is:

# serializers.py
class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = '__all__'

# views.py
class BookViewSet(viewsets.ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

# urls.py
router.register('books', BookViewSet)

Three files, three pieces of boilerplate, for every single model. humanoid_crud collapses that to:

import humanoid_crud
humanoid_crud.register(Book)

Quick start

  1. pip install humanoid-crud
  2. Add 'rest_framework' to INSTALLED_APPS in settings.py if it isn't already there.
  3. Create a small file — e.g. myapp/api.py — where you register your models:
# myapp/api.py
import humanoid_crud
from myapp.models import Book, Author

humanoid_crud.register(Author)
humanoid_crud.register(Book)
  1. In your project's urls.py, import that file (so the registrations run) and add the generated routes:
from django.contrib import admin
from django.urls import path

from myapp import api          # running this import triggers registration
import humanoid_crud

urlpatterns = [
    path('admin/', admin.site.urls),
] + humanoid_crud.urls

That's it. You now have, for each model:

Method URL Action
GET /books/ list
POST /books/ create
GET /books/<pk>/ retrieve
PUT /books/<pk>/ full update
PATCH /books/<pk>/ partial update
DELETE /books/<pk>/ delete

All validation, 404 handling, and FK integrity checks are standard DRF behavior — nothing is hidden or changed, just wired up for you.

Going beyond the default

Every option is optional — pass only what you need:

from rest_framework.permissions import IsAuthenticated

humanoid_crud.register(
    Book,
    fields=["title", "author", "price"],     # restrict exposed fields
    read_only_fields=["created_at"],
    permissions=[IsAuthenticated],            # default is AllowAny — lock this down!
    search_fields=["title"],                  # enables ?search=
    ordering_fields=["price", "created_at"],  # enables ?ordering=
    filterset_fields=["author"],              # exact-match filtering (needs the [filters] extra)
    lookup="slug",                            # use a non-pk field in the URL
    url="library-books",                      # custom URL segment
)

Escape hatch: bring your own serializer or viewset

If one model needs custom logic (e.g. a custom create(), extra validation, nested writes), you don't lose humanoid_crud's URL registration — just hand it your own class:

class BookViewSet(viewsets.ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

    def perform_create(self, serializer):
        serializer.save(added_by=self.request.user)

humanoid_crud.register(Book, viewset_class=BookViewSet)

The generated classes are ordinary ModelSerializer / ModelViewSet subclasses — if you ever outgrow the one-liner for a specific model, you write that one model the normal DRF way and humanoid_crud still handles the routing.

Important: default permissions are wide open

If you don't pass permissions=[...], every registered model defaults to AllowAny — anyone can read and write it, no login required. Fine for local prototyping. Set explicit permissions before deploying anything real.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

humanoid_crud-0.1.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

humanoid_crud-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file humanoid_crud-0.1.0.tar.gz.

File metadata

  • Download URL: humanoid_crud-0.1.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for humanoid_crud-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3511f407c4d938a059bd73bdd8d262e7e386b68ac0c3dbe6361af01b3bbcfbcd
MD5 394503baf276f1703a9859368d69553c
BLAKE2b-256 53ace8999043cadc08a19167a244a34d76e2b6963bdb8498a3cc437a8e8d3f90

See more details on using hashes here.

File details

Details for the file humanoid_crud-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: humanoid_crud-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for humanoid_crud-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f8453924ccf774bd66742222dafd456ffede49f37167e2297faf1cd4d58578c
MD5 65bbd7d65c1669e8275255ed230d6c03
BLAKE2b-256 f171ecb9953ca36b8201c84dab2d42ebe84e77139fa229616de7b88e57267402

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page