ViewSets¶
Currently only provides ModelViewSet for model-based data manipulation.
To use, import like so:
from dynamicforms.viewsets import ModelViewSet
Make sure you don’t import DRF’s ModelViewSet over this one.
Class reference¶
-
class
ModelViewSet
(*args, **kwds)[source]¶ In addition to all the functionality, provided by DRF, DynamicForms ViewSet has some extra features:
Separate templates for rendering list or single record
You can request a “new” record and even have it pre-populated with values
To render viewset as API or JSON use the same method as in DRF: To render it in HTML just add “.html” to the URL.
Standard DRF router URL patterns apply:
- To render a new record use pk=new.
- To render an existing record (for editing) use pk={record_id}.
-
new_object
()¶ Returns a new model instance. If you need it pre-populated with default values, this is the method to override.
Returns: model instance
-
filter_queryset
(queryset, query_params=None)[source]¶ Applies filters for all fields
Parameters: - queryset – Queryset
- query_params – Custom query_params if needed
Returns: queryset with filters applied
-
filter_queryset_field
(queryset, field, value)[source]¶ Applies filter to individual field
Parameters: - queryset – Queryset
- field – Field name
- value – Field value
Returns: queryset with applied filter for the field
-
static
generate_paged_loader
(page_size: int = 30, ordering: Union[str, List[str]] = 'id')[source]¶ Generates a Pagination class that will handle dynamic data loading for ViewSets with a lot of data. Use by declaring pagination_class = ModelViewSet.generate_paged_loader() in class variables
Parameters: - page_size – how many records should be fetched at a time
- ordering – This should be a string, or list of strings, indicating the field against which the cursor based pagination will be applied. For example: ordering = ‘slug’
Returns: a Pagination class