rest-framework-generic-relations
Generic Relations for Django Rest Framework
What it is and what it does
This package extends Django REST Framework with serializers for handling generic foreign keys—the polymorphic relationships Django allows via ContentType and object_id pairs. It provides two main components: one maps model types to their respective serializers for serializing a single generic relationship to any of several possible models, and another handles serializing mixed lists of different model instances, delegating to the appropriate serializer based on each object's type.
The library is designed for scenarios where a single model field points to multiple unrelated model types. It handles both read and write operations, though write support relies on the nested serializers' ability to deserialize. The package is stable and maintained for bug fixes and compatibility updates, though no major feature development is planned.
Use it for:
- Tag or annotation systems where tags can be attached to multiple unrelated model types.
- Activity feeds or audit logs that record actions on different model types in a single list.
- API endpoints returning mixed collections of different model types.
- Polymorphic relationships where a single foreign key field must point to one of several model types.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Provides Django REST Framework serializers that handle Django's generic foreign keys, allowing you to serialize and deserialize polymorphic relationships across arbitrary model types.
Yes, if you need to serialize Django generic foreign keys in REST APIs. The package is well-suited for its narrow purpose, has no known vulnerabilities, low install friction, and permissive licensing. The dormant maintenance status is not a concern for a stable, focused library—it indicates the maintainer will fix bugs and keep compatibility current rather than add new features. Use it when your API design genuinely requires polymorphic relations.
Install
rest-framework-generic-relations on PyPI
pip
pip install rest-framework-generic-relationsuv
uv add rest-framework-generic-relationspoetry
poetry add rest-framework-generic-relationsInstalling rest-framework-generic-relations
Before you install
Low friction install with a single runtime dependency on djangorestframework. Maintenance is dormant—the maintainer explicitly states no major changes are planned, though reported bugs are addressed and compatibility is kept current with recent Python and Django versions.
License in practice
Licensed under BSD (permissive), so you can use, modify, and distribute freely with minimal restrictions.
Quickstart
pip install rest-framework-generic-relations
# In Django settings.py
INSTALLED_APPS = (
...,
'rest_framework_generic_relations',
)
# In your serializer
from rest_framework_generic_relations.relations import GenericRelatedField
class TagSerializer(serializers.ModelSerializer):
tagged_object = GenericRelatedField({
Bookmark: BookmarkSerializer(),
Note: NoteSerializer()
})
class Meta:
model = TaggedItem
fields = ('tag_name', 'tagged_object')
Requires Django 3.2+, Django REST Framework 3.12+, and Python 3.8+. You must add the package to INSTALLED_APPS.
Verify before relying
- Whether write support for generic foreign keys works reliably with custom deserializer logic beyond HyperlinkedRelatedField.
- Performance characteristics when serializing large lists of mixed model types.
Package facts
| License | not declared (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — djangorestframework |
| Maintenance | dormant — 870 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 123,619/month — #11,903 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: rest_framework_generic_relations-2.2.0-py3-none-any.whl
Tags
More Dynamic Content packages
MarkupSafe provides a text object that escapes…
permissive · top 100 on PyPI
Jinja2Jinja2 is a templating engine that renders…
permissive · top 100 on PyPI
soupsieveSoupsieve is a CSS selector library designed to…
permissive · top 100 on PyPI
WerkzeugWerkzeug is a WSGI utility library providing…
permissive · top 1,000 on PyPI
FlaskFlask is a lightweight WSGI web application…
permissive · top 1,000 on PyPI
MakoMako compiles Python-embedded templates into…
permissive · top 1,000 on PyPI
drf-writable-nestedExtends Django REST Framework serializers to…
permissive · top 15,000 on PyPI
django-rest-polymorphicProvides polymorphic serializers for Django…
permissive · top 5,000 on PyPI
drf-rw-serializersExtends Django REST Framework with separate…
permissive · top 15,000 on PyPI
django-modelclusterdjango-modelcluster extends Django models to…
permissive · top 5,000 on PyPI
swapperSwapper provides an API for Django's…
permissive · top 15,000 on PyPI
drf-extra-fieldsProvides specialized serializer fields for…
permissive · top 15,000 on PyPI
drf-dynamic-fieldsProvides a Django REST Framework mixin that…
permissive · top 15,000 on PyPI
djangorestframework-dataclassesAutomatically generates Django REST Framework…
permissive · top 5,000 on PyPI
djangorestframework-bulkAdds bulk create, update, and delete operations…
permissive · top 15,000 on PyPI
drf-nested-routersProvides routers and serializer fields for…
permissive · top 5,000 on PyPI