Skip to content

Model Notes


Model Notes are a core feature that is intended to be used as the base for enabling a model to have notes assosiated to it.

Adding Notes to a Model

Most of the work has already been done, all that is required to add notes to a model is the creation of the following:

  • Model

  • Serializer

  • Viewset

Model

The model is a proxy model for core.models.model_notes.ModelNotes. This model requires a models.ForeignKey field named model to the model that will be receinving the notes.

Example model

This example is for adding notes to the Manufacturer Model

models/manufacturer_notes.py
from django.db import models

from core.models.manufacturer import Manufacturer
from core.models.model_notes import ModelNotes



class ManufacturerNotes(
    ModelNotes
):


    class Meta:

        db_table = 'core_manufacturer_notes'

        ordering = ModelNotes._meta.ordering

        verbose_name = 'Manufacturer Note'

        verbose_name_plural = 'Manufacturer Notes'


    model = models.ForeignKey(
        Manufacturer,
        blank = False,
        help_text = 'Model this note belongs to',
        null = False,
        on_delete = models.CASCADE,
        related_name = 'notes',
        verbose_name = 'Model',
    )

    table_fields: list = []

    page_layout: dict = []


    def get_url_kwargs(self) -> dict:

        return {
            'model_id': self.model.pk,
            'pk': self.pk
        }

Serializer

The Serializer contains the items the parent serializer (core.serializers.model_notes) does not have. This serializer requires the standard serializers be created that ALL inherit from the model notes parent serializer.

Example Serializer

This example is for adding notes to the Manufacturer Model

serializers/manufacturer_notes.py
from core.models.manufacturer_notes import ManufacturerNotes

from core.serializers.model_notes import (
    ModelNotes,
    ModelNoteBaseSerializer,
    ModelNoteModelSerializer,
    ModelNoteViewSerializer
)



class ManufacturerNoteBaseSerializer(ModelNoteBaseSerializer):

    pass


class ManufacturerNoteModelSerializer(
    ModelNoteModelSerializer
):


    class Meta:

        model = ManufacturerNotes

        fields =  ModelNoteModelSerializer.Meta.fields + [
            'model',
        ]

        read_only_fields = ModelNoteModelSerializer.Meta.read_only_fields + [
            'model',
            'content_type',
        ]



class ManufacturerNoteViewSerializer(
    ModelNoteViewSerializer,
    ManufacturerNoteModelSerializer,
):

    pass

ViewSet

The ViewSet is a class that inherits from the Model Notes base ViewSet class, core.viewsets.model_notes.ViewSet. The ViewSet is responsible for providing the Serializer, Documentation (Swagger UI) and Providing the Model.

Example ViewSet

This example is for adding notes to the Manufacturer Model

viewsets/manufacturer_notes.py
from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiResponse

from core.serializers.manufacturer_notes import (
    ManufacturerNotes,
    ManufacturerNoteModelSerializer,
    ManufacturerNoteViewSerializer,
)

from core.viewsets.model_notes import ModelNoteViewSet



@extend_schema_view(
    create=extend_schema(
        summary = 'Add a note to a Manufacturer',
        description = '',
        responses = {
            201: OpenApiResponse(description='created', response=ManufacturerNoteViewSerializer),
            400: OpenApiResponse(description='Validation failed.'),
            403: OpenApiResponse(description='User is missing create permissions'),
        }
    ),
    destroy = extend_schema(
        summary = 'Delete a Manufacturer note',
        description = ''
    ),
    list = extend_schema(
        summary = 'Fetch all Manufacturer notes',
        description='',
    ),
    retrieve = extend_schema(
        summary = 'Fetch a single Manufacturer note',
        description='',
    ),
    update = extend_schema(exclude = True),
    partial_update = extend_schema(
        summary = 'Update a Manufacturer note',
        description = ''
    ),
)
class ViewSet(ModelNoteViewSet):

    model = ManufacturerNotes


    def get_serializer_class(self):

        if self.serializer_class is not None:

            return self.serializer_class


        if (
            self.action == 'list'
            or self.action == 'retrieve'
        ):

            self.serializer_class = ManufacturerNoteViewSerializer


        else:

            self.serializer_class = ManufacturerNoteModelSerializer

        return self.serializer_class

Testing

As with any other object within Centurion, the addition of a feature requires it be tested. The following Test Suites are available:

  • Unit - core.tests.abstract.test_unit_model_notes_model.ModelNotesModel

  • Unit - core.tests.abstract.test_unit_model_notes_serializer.ModelNotesSerializerTestCases

  • Unit - api.tests.abstract.viewsets

  • Functional - core.tests.abstract.test_functional_notes_viewset

  • Functional - core.tests.abstract.model_notes_api_fields

About:

This page forms part of our Project Centurion ERP.

Page Metadata
Version: ToDo: place files short git commit here
Date Created: 2025-02-09
Date Edited: 2025-02-09

Contribution:

Would You like to contribute to our Centurion ERP project? You can assist in the following ways:

 

ToDo: Add the page list of contributors