django-service-objects

raw JSON →
0.7.1 verified Fri May 01 auth: no python

A Django library that provides a service layer pattern for encapsulating business logic and operations on models. It offers Service and ModelService base classes with built-in form processing, validation, and optional Celery integration. Current version 0.7.1 supports Django 4.0 and Python 3.10; releases are infrequent.

pip install django-service-objects
error ModuleNotFoundError: No module named 'service_objects'
cause The package is not installed or virtual environment not activated.
fix
Run: pip install django-service-objects && python -c "import service_objects"
error AttributeError: 'MyService' object has no attribute 'cleaned_data'
cause Accessing cleaned_data before the service is processed or without calling is_valid().
fix
Call MyService.execute(input_data) which automatically validates and processes, or manually call .is_valid() before .cleaned_data.
breaking Service class changed from a plain class to an Abstract Base Class (ABC) in 0.5.0. If you have custom __init__ methods, they may break if they don't call super().__init__().
fix Ensure your service subclass calls super().__init__(*args, **kwargs).
deprecated ListField and DictField are deprecated in 0.7.0; the library suggests using standard Django JSONField or custom fields instead.
fix Replace ListField/DictField with Django's JSONField or implement custom fields.
gotcha The process() method is not called inside a database transaction by default unless db_transaction=True is set on the Service Meta.
fix Set class Meta: db_transaction = True on your service to wrap process() in a transaction.

Define a Service class with form fields and a process method, then call MyService.execute(dict).

from django import forms
from service_objects.services import Service

class MyForm(forms.Form):
    name = forms.CharField()

class MyService(Service):
    name = forms.CharField()

    def process(self):
        return f"Hello {self.cleaned_data['name']}"

# Usage:
# result = MyService.execute({'name': 'World'})
# print(result)  # 'Hello World'