Django REST Multiple Models
raw JSON → 2.1.3 verified Fri May 01 auth: no python
Provides a generic view (and mixin) for returning multiple models or querysets from a single Django REST Framework endpoint. Current version 2.1.3. Maintenance appears sporadic.
pip install django-rest-multiple-models Common errors
error ModuleNotFoundError: No module named 'drf_multiple_model' ↓
cause Package not installed or installed under a different name.
fix
Run 'pip install django-rest-multiple-models' and verify installation.
error AttributeError: module 'drf_multiple_model.views' has no attribute 'MultipleModelAPIView' ↓
cause Upgraded from v1.x to v2.x without updating import.
fix
Use 'ObjectMultipleModelAPIView' instead.
error ImproperlyConfigured: Each item in querylist must have 'queryset' and 'serializer_class' keys. ↓
cause Missing required keys in querylist dict.
fix
Ensure each dict contains 'queryset' and 'serializer_class'.
error AssertionError: Expected view to have attribute 'sort_fn' or 'sort_field' if 'sort_url_param' is set. ↓
cause Trying to use sorting without defining sorting logic.
fix
Set 'sort_fn' or 'sort_field' on the view when using 'sort_url_param'.
Warnings
breaking v2.0 renamed MultipleModelAPIView to ObjectMultipleModelAPIView, and removed the old mixin-based API. Update imports accordingly. ↓
fix Replace 'MultipleModelAPIView' with 'ObjectMultipleModelAPIView'. See changelog for full migration.
breaking Pagination in v2.0 changed from a global setting to a per-querylist attribute. The old 'pagination_count' attribute is removed. ↓
fix Set 'pagination' inside each querylist dict (or use class-level 'pagination'). See docs.
gotcha The 'flat' property (FlatMultipleModelAPIView) returns all items in a single list. Without 'flat', each label is a key with a list. Ensure client expectations match. ↓
fix Use 'FlatMultipleModelAPIView' when a flat list is desired.
Imports
- MultipleModelMixin
from drf_multiple_model.views import MultipleModelMixin - MultipleModelViewSetMixin
from drf_multiple_model.views import MultipleModelViewSetMixin - ObjectMultipleModelAPIView wrong
from drf_multiple_model.views import MultipleModelAPIViewcorrectfrom drf_multiple_model.views import ObjectMultipleModelAPIView - FlatMultipleModelAPIView
from drf_multiple_model.views import FlatMultipleModelAPIView
Quickstart
# views.py
from drf_multiple_model.views import ObjectMultipleModelAPIView
from .models import Author, Book
from .serializers import AuthorSerializer, BookSerializer
class MixedAPIView(ObjectMultipleModelAPIView):
querylist = [
{'queryset': Author.objects.all(), 'serializer_class': AuthorSerializer},
{'queryset': Book.objects.all(), 'serializer_class': BookSerializer, 'label': 'books'},
]
# Optional: sorting, pagination, etc.