Skip to main content

Django CRUDViewSet

Project description

Django CRUDViewSet

Requirements

  • Python 3.6.*
  • Django 2.*

Installation

Install using pip ...

pip install django-crudviewset

Add 'crudviewset' to your INSTALLED_APPS setting.

INSTALLED_APPS = (
    ...
    'crudviewset',
)

Example

Simple Example

example/views.py

from crudviewset import CRUDViewSet

from example.models import Board

BoardCRUDViewSet = CRUDViewSet('board', Board)

example/urls.py

from . import views

urlpatterns = []
urlpatterns.extend(views.BoardCRUDViewSet.get_urlpatterns())

Custom Example

example/views.py

from django.contrib.auth.mixins import UserPassesTestMixin

from crudviewset import CRUDViewSet, ViewParams, VIEWKIND

from example.models import Board
from example.forms import BoardForm


class BoardFormViewMixin:
    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs


BoardCRUDViewSet = CRUDViewSet(
    'board', Board, app_name='example',
    base_superclasses=[UserPassesTestMixin],
    base_variables={
        'test_func': lambda self: self.request.user.is_authenticated,
        'get_queryset': lambda self: Board.objects.filter(user=self.request.user).order_by('-created_at'),
    },
    list_vp=ViewParams(
        role=VIEWKIND.LIST,
        variables={
            'paginate_by': 10,
        }
    ),
    detail_vp=ViewParams(
        role=VIEWKIND.DETAIL,
        superclasses=[],
        variables={
            'get_queryset': lambda self: Board.objects.all().order_by('-created_at'),
        }
    ),
    create_vp=ViewParams(
        role=VIEWKIND.CREATE,
        superclasses=[UserPassesTestMixin, BoardFormViewMixin],
        variables={
            'form_class': BoardForm,
            'template_name': 'example/example_form.html',
        }
    ),
    update_vp=ViewParams(
        role=VIEWKIND.UPDATE,
        superclasses=[UserPassesTestMixin, BoardFormViewMixin],
        variables={
            'form_class': BoardForm,
            'template_name': 'example/example_form.html',
        }
    ),
    delete_vp=ViewParams(
        role=VIEWKIND.DELETE,
        variables={
            'success_url': reverse_lazy('main:example_list'),
        }
    ),
    vp_list=[
        ViewParams(
            role=VIEWKIND.LIST,
            route='latest/',
            route_name='latest',
            variables={
                'paginate_by': 10,
                'template_name': 'example/latest.html',
                'get_queryset': lambda self: Board.objects.all().order_by('-created_at'),
            }
        ),
    ]
)

example/urls.py

from . import views

app_name = 'example'
urlpatterns = []
urlpatterns.extend(views.BoardCRUDViewSet.get_urlpatterns())

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

django-crudviewset-0.1.2.tar.gz (6.4 kB view hashes)

Uploaded source

Built Distribution

django_crudviewset-0.1.2-py3-none-any.whl (7.8 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page