Skip to main content

Django Rest Framework SCRUD viewset.

Project description

DRF SCRUD Viewset

DRF SCRUD (emphasizes on the S, you'll discover why) Viewset is a lightweight Django package built on top of django-rest-framework. It delivers a highly abstracted global viewset thats provides common CRUD operations methods including advanced search and pagination.

Features

  • Create, Read, Update and Delete ready-to-go methods for viewsets
  • FileUploadParser enabled for create and edit methods to allow uploading files and images
  • Powerful search feature (all its greatness described bellow )
  • Toggle status of instances (if is_active field on model)
  • Paginated responses

Install

pip install drf-scrud

Quick Start

  1. Add scrud to your INSTALLED_APPS like this:
    INSTALLED_APPS = [
        ...
        'scrud',  
    ] ```
    
  2. Use in views.py:
    from scrud import ScrudViewset
    from . import models, serializers
    
    class BookViewset(ScrudViewset)
        # Override the default permissions by action if needed. Default is AllowAny for all actions.
        
        permission_classes_by_action = {
            'create': [IsAuthenticated],
            'list': [IsAuthenticated],
            'get': [IsAuthenticated],
            'edit': [IsAuthenticated],
            'delete': [IsAdminUser],
            'active': [IsAdminUser],
            'desactive': [IsAdminUser],
            'inactives': [IsAdminUser],
            'search': [IsAuthenticated]
        }
        def __init__(self):
            super().__init__(models.Book, serializers.BookSerializer, self.permission_classes_by_action)
    

When defining your Viewset this way, BookViewset inherit these methods: list, create, get, edit, delete, activate, deactivate, search

  1. Then, in urls.py you can set:
    path('book/', include([
        path('', views.BookViewset.as_view({'get': 'list', 'post': 'create'})),
        path('<int:pk>/', views.BookViewset.as_view({'get': 'get'})),
        path('<int:pk>/edit/', views.BookViewset.as_view({'put': 'edit'})),
        path('<int:pk>/delete/', views.BookViewset.as_view({'delete': 'delete'})),
        path('search/', views.BookViewset.as_view({'get': 'search'})),
    ])),
        
  1. DRF Settings

The returned json of all the endpoints are page number paginated, so you may need to set a default PAGE_SIZE in settings.py

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10
}
  1. You are done ! All the endpoints should be working fine by now.

Especialy for the 'inactives()' action

This action make use of a customized manager that binds active_rows() and inactive_rows() to model.objects, like Book.objects.inactive_rows()

So if you intend to use this action, consider adding the manager in your models.py like this:

from scrud.managers import TemporalQuerySet

class BookModel(models.Model):
    ...
    objects = TemporalQuerySet()

The Search Action

This action implements advanced query filtering throught model instances to help you improve your API.

https://YOUR_API_ENDPOINT/search/?field_name=value

The field_name can be any of the model field.

Considering a model represented by this json

{
    "id": 1,
    "title": "The journey to Elixir",
    "description": "Elixir is going to rule the world. You better watch out !",
    "price": 250,
    "currency": "USD",
    "release_date": "2022-10-10",
    "author": {
        "id": 1,
        "firstName": "John",
        "lastName": "Doe",
        "city": {
            "id": 5,
            "name": "Sydney",
            "country": {
                "id": 2,
                "name": "Australia"
            }
        }
    } 
}

You can filter by title, description and/or any other /search/?title="stuff" or /search/?title="elix"&description="world"&price=150&{and_so_on_to_infinity}

String fields are double-quoted

  • Even better

    You can lookup throughout related models

    /search/?author__city__country_name="Aus"

      Double underscore __ to reach related model name, and single _ to reach a field
    

    String field lookups are case insensitive and perform a like %% sql query

Overriding a method

You can override any of the pre-built method.

  • e.g adding decorator
@extend_schema(
        parameters=[
            OpenApiParameter(
            name='title',
            required=False,
            location=OpenApiParameter.QUERY,
            ),
        ]
    )
    def search(self, request):
        return super().search(request)
  • If you want to override the entire function, just write your function as usual
def search(self, request):
    # New code goes here

Feel free to open issues or to pull request. Contributors are welcome.

© Shuruzer.

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

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

Source Distribution

drf-scrud-1.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

drf_scrud-1.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file drf-scrud-1.0.tar.gz.

File metadata

  • Download URL: drf-scrud-1.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.0

File hashes

Hashes for drf-scrud-1.0.tar.gz
Algorithm Hash digest
SHA256 52a3ab44cb73f49cdec0f5a428a09c18e60ce28515e7129f51f75a86146ab770
MD5 706bf8f3debfb9630533da189880bd64
BLAKE2b-256 25711f7a9df66f9a7e10d5b4b7445335987803f4a570f6af040c87a607b7d080

See more details on using hashes here.

File details

Details for the file drf_scrud-1.0-py3-none-any.whl.

File metadata

  • Download URL: drf_scrud-1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.0

File hashes

Hashes for drf_scrud-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 041404376a89964ea28248beb0a7de0a4d487ea7439aafa6eb5065c48a0cd69a
MD5 b512043d478d398eecb9ad296d6e37e8
BLAKE2b-256 5c4a316ef77ee1f37cab3b69b88eadc804c500dbc5eac1de1c491693da149bcd

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