feat(api): when fetching related_object, default to base_model for SubModelViewSet

ref: #732 #726
This commit is contained in:
2025-04-26 06:13:39 +09:30
parent 677cc55e04
commit c152dd13f1
2 changed files with 42 additions and 9 deletions

View File

@ -815,8 +815,14 @@ class SubModelViewSet(
break
if related_model is None:
related_model = self.base_model
return related_model
def get_serializer_class(self):
serializer_name = self.base_model._meta.verbose_name.lower().replace(' ', '_')

View File

@ -6,7 +6,7 @@ template: project.html
about: https://gitlab.com/nofusscomputing/infrastructure/configuration-management/centurion_erp
---
Views are used with Centurion ERP to Fetch the data for rendering.
Viewsets are used by Centurion ERP for each of the API views.
!!! info
Centurion release v1.3.0 added a feature lock to **ALL** Views and the current API. From this release, there is a new API at endpoint `api/v2`. As such we will only be using DRF `ViewSets`. This is required as the UI is being separated from the Centurion Codebase to its own repository. This means that Centurion will become an API only codebase. Release 2.0.0 will remove the current UI and api from Centurion. [See #](https://github.com/nofusscomputing/centurion_erp/issues/343) for details.
@ -16,14 +16,6 @@ Views are used with Centurion ERP to Fetch the data for rendering.
- Views are class based
- Inherits from one of the following base class':
- Index Viewset `api.viewsets.common.CommonViewSet`
- Model Viewset `api.viewsets.common.ModelViewSet`
- Model Viewset that are to be Read-Only `api.viewsets.common.ReadOnlyModelViewSet`
- **ALL** views are `ViewSets`
- Views are saved within the module the model is from under path `viewsets/`
@ -51,6 +43,41 @@ Views are used with Centurion ERP to Fetch the data for rendering.
- ViewSets that are used to expose data that is publicly available **must** have it's filename prefixed with `public_`
## Creating a ViewSet
All ViewSets are to be saved under the django app they belong to and within a directory called `viewsets`. Serializers are broken down to match the [model types](./models.md#creating-a-model):
### Standard Model ViewSet
#### Requirements
- Inherits from one of the following base class':
- Index ViewSet `api.viewsets.common.CommonViewSet`
- Model ViewSet that are to be Read-Only `api.viewsets.common.ReadOnlyModelViewSet`
- If not any of the above, `api.viewsets.common.ModelViewSet`
### Sub-Model ViewSet
#### Requirements
- Attribute 'base_model' must be specified within the ViewSet
- ViewSet must inherit from `api.viewsets.common.SubModelViewSet`
- Tested against:
- Unit Tests:
- `api.tests.unit.test_unit_common_viewset.SubModelViewSetInheritedCases`
## Permissions
If you wish to deviate from the standard CRUD permissions, define a function called `get_dynamic_permissions` within the `view`/`ViewSet`. The function must return a list of permissions. This is useful if you have added additional permissions to a model.