Django Chain Models¶
django_chain.mixins
¶
Mixins for reusable view logic in django-chain.
This module provides mixins for JSON response handling, model CRUD operations, and activation/deactivation patterns to be used in Django class-based views throughout django-chain.
Typical usage example
class MyView(JSONResponseMixin, ModelListMixin, View): ...
LLMGenerationMixin
¶
Mixin for LLM model and workflow instantiation in views.
Provides methods to get a model, invoke a runnable, and dispatch requests for LLM generation.
get_model(**kwargs)
¶
Get a workflow chain for LLM generation.
Parameters:
-
**kwargs
–Arguments for prompt, model, and workflow selection.
Returns:
-
Any
(Any
) –The workflow chain instance.
invoke_runnable(request, *args, **kwargs)
¶
Invoke a runnable with user input.
Parameters:
-
request
(HttpRequest
) –The HTTP request object.
-
*args
–Additional arguments.
-
**kwargs
–Must include 'runnable' and 'input'.
Returns:
-
Any
(Any
) –The output from the runnable.
dispatch(request, *args, **kwargs)
¶
Custom dispatch to handle LLM invocation for POST requests.
Parameters:
-
request
(HttpRequest
) –The HTTP request object.
-
*args
–Additional arguments.
-
**kwargs
–Additional keyword arguments.
Returns:
-
HttpResponse
–The response from the view.
JSONResponseMixin
¶
Mixin to render a response as JSON. Handles serialization of data and consistent error responses.
render_json_response(data=None, status=200, safe=True)
¶
Renders data as a JSON response.
safe=False
is needed if the top-level object is a list.
Parameters:
-
data
(dict
, default:None
) –Data to serialize. Defaults to None.
-
status
(int
, default:200
) –HTTP status code. Defaults to 200.
-
safe
(bool
, default:True
) –Whether to allow non-dict top-level objects. Defaults to True.
Returns:
-
JsonResponse
(JsonResponse
) –The JSON response.
json_error_response(error_message, status=400)
¶
Returns a consistent JSON error response.
Parameters:
-
error_message
(str | dict
) –The error message or dict of errors.
-
status
(int
, default:400
) –HTTP status code. Defaults to 400.
Returns:
-
JsonResponse
(JsonResponse
) –The JSON error response.
dispatch(request, *args, **kwargs)
¶
Apply csrf_exempt to all methods in views inheriting this mixin. Also attempts to load JSON body for POST/PUT methods.
Parameters:
-
request
(HttpRequest
) –The HTTP request object.
-
*args
–Additional arguments.
-
**kwargs
–Additional keyword arguments.
Returns:
-
HttpResponse
–The response from the view.
ModelRetrieveMixin
¶
Mixin for retrieving a single model instance by its primary key (pk).
Requires: model_class
, serializer_method
.
get_object(pk)
¶
Retrieve a model instance by primary key.
Parameters:
-
pk
(Any
) –The primary key value.
Returns:
-
Any
(Any
) –The model instance or None if not found.
ModelListMixin
¶
Mixin for listing model instances with optional filtering.
Requires: model_class
, serializer_method
.
get_queryset(request)
¶
Get a queryset of all model instances.
Parameters:
-
request
(HttpRequest
) –The HTTP request object.
Returns:
-
QuerySet
(Any
) –The queryset of model instances.
apply_list_filters(queryset, request)
¶
Apply filters to the queryset. Override in subclasses.
Parameters:
-
queryset
(QuerySet
) –The queryset to filter.
-
request
(HttpRequest
) –The HTTP request object.
Returns:
-
QuerySet
(Any
) –The filtered queryset.
ModelCreateMixin
¶
Mixin for handling POST requests to create new model instances.
Requires: model_class
, serializer_method
.
create_object(request_data)
¶
Create a new model instance from request data.
Parameters:
-
request_data
(dict
) –Data for the new instance.
Returns:
-
Any
(Any
) –The created model instance.
ModelUpdateMixin
¶
Mixin for handling PUT requests to update existing model instances.
Requires: model_class
, serializer_method
.
update_object(obj, request_data)
¶
Update an existing model instance with request data.
Parameters:
-
obj
(Any
) –The model instance to update.
-
request_data
(dict
) –Data to update the instance with.
Returns:
-
Any
(Any
) –The updated model instance.
ModelDeleteMixin
¶
Mixin for handling DELETE requests to delete model instances.
delete_object(obj)
¶
Delete a model instance.
Parameters:
-
obj
(Any
) –The model instance to delete.
ModelActivateDeactivateMixin
¶
Mixin for handling activation/deactivation of models with an 'is_active' field.
Requires: model_class
, serializer_method
.
post(request, pk, action)
¶
Activate or deactivate a model instance.
Parameters:
-
request
(HttpRequest
) –The HTTP request object.
-
pk
(Any
) –The primary key of the instance.
-
action
(str
) –'activate' or 'deactivate'.
Returns:
-
JsonResponse
(Any
) –The response with the updated instance or error.