Skip to main content

A Django app providing reusable abstract models.

Project description

About

This library creates Abstract models which will be used to reduce work across all the models of your application. You can just import the library and inherit it in other models to make usage of its functionality.

Usage

from django_auditable_models.models import AbstractAuditableModel

class MyModel(models.Model,AbstractAuditableModel):
    name = models.CharField(max_length = 150,null=True,blank=True)
    
test = MyModel()
test.name = 'TestName'
test.save()

print(test.created_on)

Use the Mixin in Your Serializers

When defining serializers for your models, inherit from both AuditableModelMixin and serializers.ModelSerializer (or any other base serializer class you're using). The AuditableModelMixin ensures that the created_by field is automatically populated from the request context.

from myapp.models import MyModel
from rest_framework import serializers
from django_auditable_models.serializers import AuditableModelMixin

class MyModelSerializer(AuditableModelMixin, serializers.ModelSerializer):
    class Meta:
        model = MyModel
        fields = ['id', 'name', 'created_by']
        read_only_fields = ('created_by',)

Ensure the Request Context is Passed to the Serializer

In your views, ensure that the serializer's context includes the request object. This is automatically handled if you're using DRF's generic views or viewsets.

from myapp.models import MyModel
from myapp.serializers import MyModelSerializer
from rest_framework import generics

class MyModelListCreateAPIView(generics.ListCreateAPIView):
    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer

If you're not using DRF's view classes that automatically pass the context, make sure to include the request context manually when initializing the serializer.

serializer = MyModelSerializer(data=request.data, context={'request': request})

Advantages

  • Separation of Concerns: This approach keeps the logic for assigning the created_by field within the API layer, maintaining a clear separation from the model layer.
  • Flexibility and Reusability: By using a mixin, you can easily apply this functionality to any serializer without duplicating code.
  • Explicit Context Usage: It leverages the serializer context, which is explicitly designed for passing additional information like the request object, ensuring that the implementation aligns with DRF's design patterns.

This serializer-level solution is particularly suited for Django projects that use DRF for building APIs, providing a clean, reusable way to automatically handle created_by fields across different models and serializers.

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_auditable_model-0.1.4.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_auditable_model-0.1.4-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file django_auditable_model-0.1.4.tar.gz.

File metadata

  • Download URL: django_auditable_model-0.1.4.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for django_auditable_model-0.1.4.tar.gz
Algorithm Hash digest
SHA256 5b741eab81dc894dda996c3b4b6f52a97aacc679b16dca14d166a9ee417638f7
MD5 894ff49e24b4276cd81869127a307b6e
BLAKE2b-256 a1d03d43eaf68a7d96b6c2552de8644d3edd9d569e5573b8823db7439083ab6e

See more details on using hashes here.

File details

Details for the file django_auditable_model-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_auditable_model-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b81d3c7dd5df132ffdbcbf181633a09cee10c308bf46278ad81d4f3e70e2b440
MD5 ce483fb2a4fd5ee4b0be3aa4135095ec
BLAKE2b-256 18ca693d285f6a2893e21d5de27c8d397d0fc1e6d96bac8899b1dd5f8df8853e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page