menu

CRUD

material.frontend contains several class-based views, allows to build CRUD interfaces in a few lines of code.

You can start with a simple viewset - a class that collects CRUD view actions for a model in a myapp/views.py

from material.frontend.views import ModelViewSet
from . import models

class MyModelViewSet(ModelViewSet):
    model = models.MyModel

And add an entry to the myapp/urls.py

from django.conf.urls import url, include
from django.views import generic
from . import views

urlpatterns = [
    url('^$', generic.RedirectView.as_view(url='./mymodel/', permanent=False), name="index"),
    url('^mymodel/', include(views.MyModelViewSet().urls)),
    ...
]

ModelViewSet class is pretty similar to the django.contrib.admin.ModelAdmin except the all CRUD actions implemented in the separated class bases views.

API

class material.frontend.views.ModelViewSet

Model Create/Read/Update/Delete/List viewset.

Lightweight alternative for django admin. CRUD interface based in the django generic views. Viewset provides a simple place to configure the views and share the common configuration parameters.

Parameters:
  • model – Model class views
  • queryset – Base views queryset
  • list_display – List of fields for ListView
  • list_display_links – List of fields form list_display linked to change view
  • list_actions – Tuple of (‘Action Name’, ‘/action/url/’) that get called with a list of objects selected on the change list page. PRO Only
  • list_filterset_fields – List or dictinary of fields spec for django-filters in a ListView. PRO Only
  • list_filterset_class – Subclass of django_filters.FilterSet for ListView, if specified filterset_fields option ignored. PRO Only
  • create_view_class – CBV for create an object
  • list_view_class – CBV for create to list objects
  • detail_view_class – CBV to show an object detail
  • update_view_class – CBV to update an object
  • delete_view_class – CBV to delete an object
  • layout – Layout for django-material forms
  • form_class – Form Class for Create and Update views
  • form_widgets – Custom widgets for the model form in the Create and Update views, if no custom form_class provided.

There is no specifically requirements to CBV, by overriding corresponding get_<op>_view method you can even use function based views with this viewset.

Example:

class CityViewSet(ModelViewSet):
    model = models.Sea
    list_display = ('name', 'parent', 'ocean', 'sea_area', )
    layout = Layout(
        Row('name', 'parent'),
        'ocean',
        Row('area', 'avg_depth', 'max_depth'),
        'basin_countries'
    )

    def sea_area(self, sea):
        return None if sea.area == 0 else sea.area
get_queryset(self, request)

If defined, provides a default value for views queryset.

create_view

Triple (regexp, view, name) for create view url config.

create_view_class

alias of CreateModelView

delete_view

Triple (regexp, view, name) for delete view url config.

delete_view_class

alias of DeleteModelView

detail_view

Triple (regexp, view, name) for detail view url config.

detail_view_class

alias of DetailModelView

filter_kwargs(view_class, **kwargs)

Add defaults and filter kwargs to only those that view can accept.

Viewset pass to the view only kwargs that have non DEFAULT values, and if the view have a corresponding attribute.

In addition, if view has viewset attribute, it will receive the self instance

get_action_view(action_view_class)

Construct a function action view form view class.

PRO Only

get_action_view_kwargs(action_view_class, **kwargs)

Configuration arguments for action view.

PRO Only

get_create_view()

Function view for create an object.

get_create_view_kwargs(**kwargs)

Configuration arguments for create view.

May not be called if get_create_view is overridden.

get_delete_view()

Function view for delete an object.

get_delete_view_kwargs(**kwargs)

Configuration arguments for delete view.

May not be called if get_delete_view is overridden.

get_detail_view()

Function view for object detail.

get_detail_view_kwargs(**kwargs)

Configuration arguments for detail view.

May not be called if get_detail_view is overridden.

get_list_view()

Function view for objects list.

get_list_view_kwargs(**kwargs)

Configuration arguments for list view.

May not be called if get_list_view is overridden.

get_update_view()

Function view for update an object.

get_update_view_kwargs(**kwargs)

Configuration arguments for update view.

May not be called if get_update_view is overridden.

has_add_permission(request)

Default add permission check for a detail and list views.

May not be called if views have own implementation.

has_change_permission(request, obj=None)

Default change permission check for a update view.

May not be called if update view have own implementation.

has_delete_permission(request, obj=None)

Default delete permission check for a delete view.

May not be called if delete view have own implementation.

has_view_permission(request, obj=None)

Default view permission check for a detail and list views.

May not be called if views have own implementation.

list_view

Triple (regexp, view, name) for list view url config.

list_view_class

alias of ListModelView

update_view

Triple (regexp, view, name) for update view url config.

update_view_class

alias of UpdateModelView

urls

Collect url specs from the instance attributes.

Assumes that each attribute with name ending with _view contains tripple (regexp, view, url_name)

In addition registers action views on own urls.

class material.frontend.views.CreateModelView(*args, **kwargs)

Thin generic.CreateView wrapper plays nice with ModelViewSet.

get_success_url()

Redirect back to the detail view if no success_url is configured.

has_object_permission(request, obj)

Object add permission check.

If view had a viewset, the viewset.has_add_permission used.

class material.frontend.views.UpdateModelView(*args, **kwargs)

Thin generic.UpdateView wrapper plays nice with ModelViewSet.

get_success_url()

Redirect back to the detail view if no success_url is configured.

has_object_permission(request, obj)

Object change permission check.

If view had a viewset, the viewset.has_change_permission used.

class material.frontend.views.DeleteModelView(**kwargs)

View for deleting an object and all linked by foreign key data.

get_context_data(**kwargs)

Extend view context data.

{{ deleted_objects }} - list of related objects to delete

get_object()

Retrieve the object for delete.

Check object delete permission at the same time.

get_success_url()

Redirect back to the list view if no success_url is configured.

get_template_names()

List of templates for the view.

If no self.template_name defined, uses:

[<app_label>/<model_label>_delete.html,
 'material/frontend/views/confirm_delete.html']
has_object_permission(request, obj)

Object delete permission check.

If view had a viewset, the viewset.has_delete_permission used.

class material.frontend.views.DetailModelView(**kwargs)

Thin wrapper for generic.DetailView.

get_context_data(**kwargs)

Additional context data for detail view.

Parameters:
  • object_data – List of fields and values of the object
  • change_url – Link to the change view
  • delete_url – Link to the delete view
get_object()

Retrieve the object.

Check object view permission at the same time.

get_object_data()

List of object fields to display.

Choice fields values are expanded to readable choice label.

get_template_names()

List of templates for the view.

If no self.template_name defined, returns:

[<app_label>/<model_label>_detail.html
 'material/frontend/views/detail.html']
has_change_permission(request, obj)

Object chane permission check.

If view had a viewset, the viewset.has_change_permission used.

If true, view will show Change link to the Change view.

has_delete_permission(request, obj)

Object delete permission check.

If true, view will show Delete link to the Delete view.

has_view_permission(request, obj)

Object view permission check.

If view had a viewset, the viewset.has_view_permission used.

class material.frontend.views.list.DataTableMixin

Mixing for list views with DataTable.

dispatch(request, *args, **kwargs)

Handle for browser HTTP and AJAX requests from datatables.

get(request, *args, **kwargs)

Response with rendered html template.

get_columns_def()

Returns columns definition for the datables js config.

get_context_data(**kwargs)

Update view context.

Include datatable_config, ‘headers’ and initial data to first page render.

get_data_attr(attr_name)

Data getter for an attribute.

Data could comes from the model field or external data_source method call.

get_datatable_config()

Prepare datatable config.

get_headers_data()

Readable column titles.

get_json_data(request, *args, **kwargs)

Return JSONResponse with data for datatable.

get_list_display()

Return list of columns to display.

get_object_list()

Create prepared queryset for datatables view.

get_ordering()

Return the field or fields to use for ordering the queryset.

PRO Only

get_table_data(start, length)

Get a page for datatable.

total()

Total dataset size.

total_filtered()

Dataset size with filter applied.

class material.frontend.views.list.FilterMixin
get_filterset_class()

Return the filterset class to use in this view.

class material.frontend.views.ListModelView(**kwargs)

List view sutable to work with jQuery Datatables.

The view responsive for handling GET/POST requests from the browser and AJAX from the datatable.

Parameters:
  • model – Model class views
  • queryset – Base views queryset
  • paginate_by – Page size
  • ordering – Default list order
  • datatable_config – Datatable default config overrides
  • list_display – List of fields to display
  • list_display_links – List of fields form list_display linked to change view
  • filterset_fields – List or dictinary of fields spec for django-filters. See details at https://github.com/carltongibson/django-filter#usage
  • filterset_class – Subclass of django_filters.FilterSet, if specified filterset_fields option ignored.
  • list_actions – Tuple of (‘Action Name’, ‘/action/url/’) that get called with a list of objects selected on the change list page. PRO Only
dispatch(*args, **kwargs)

Handle for browser HTTP and AJAX requests from datatables.

get_context_data(**kwargs)

Additional context data for list view.

Parameters:add_url – Link to the add view
get_item_url(item)

Link to object detail to list_display_links columns.

Return columns list that would be linked to the object details.

If self.list_display_links is not set, the first column would be used.

get_queryset()

Retrieve the view queryset.

get_template_names()

List of templates for the view.

If no self.template_name defined, uses:

[<app_label>/<model_label>_list.html,
 'material/frontend/views/list.html']
has_add_permission(request)

Object add permission check.

If view had a viewset, the viewset.has_add_permission used.

has_change_permission(request, obj=None)

Object change permission check.

If view had a viewset, the viewset.has_change_permission used.

has_view_permission(request, obj=None)

Object view permission check.

If view had a viewset, the viewset.has_view_permission used.

class material.frontend.views.actions.BaseActionView(**kwargs)
get_success_url()

Redirect back to the list view if no success_url is configured.

post(request, *args, **kwargs)

Handles POST requests, instantiating a form instance with the passed POST variables and then checked for validity.

class material.frontend.views.actions.DeleteActionView(**kwargs)
get_context_data(**kwargs)

Extend view context data.

{{ deleted_objects }} - list of related objects to delete

get_template_names()

List of templates for the view.

If no self.template_name defined, returns:

[<app_label>/<model_label>_delete_action.html
 'material/frontend/views/delete_action.html']