skillfed

rest-framework-generic-relations

Generic Relations for Django Rest Framework

rest-framework-generic-relations v2.2.0 123.6K downloads/30d#11,903 on PyPI7
Permissive license DORMANT released

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-relations

uv

uv add rest-framework-generic-relations

poetry

poetry add rest-framework-generic-relations

Installing 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

Environment :: Web EnvironmentFramework :: DjangoFramework :: Django :: 3.2Framework :: Django :: 4.2Framework :: Django :: 5.0Intended Audience :: DevelopersLicense :: OSI Approved :: BSD LicenseOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Programming Language :: Python :: Implementation :: CPythonProgramming Language :: Python :: Implementation :: PyPy

Tags

django generic foreign key serializerrest framework polymorphic relationsdjango rest framework generic relationsserialize generic foreign keys drfpolymorphic model serialization djangogeneric relation serializer
django-rest-frameworkserializationpolymorphic-relations

More Dynamic Content packages

MarkupSafe

MarkupSafe provides a text object that escapes…

permissive · top 100 on PyPI

Jinja2

Jinja2 is a templating engine that renders…

permissive · top 100 on PyPI

soupsieve

Soupsieve is a CSS selector library designed to…

permissive · top 100 on PyPI

Werkzeug

Werkzeug is a WSGI utility library providing…

permissive · top 1,000 on PyPI

Flask

Flask is a lightweight WSGI web application…

permissive · top 1,000 on PyPI

Mako

Mako compiles Python-embedded templates into…

permissive · top 1,000 on PyPI

drf-writable-nested

Extends Django REST Framework serializers to…

permissive · top 15,000 on PyPI

django-rest-polymorphic

Provides polymorphic serializers for Django…

permissive · top 5,000 on PyPI

drf-rw-serializers

Extends Django REST Framework with separate…

permissive · top 15,000 on PyPI

django-modelcluster

django-modelcluster extends Django models to…

permissive · top 5,000 on PyPI

swapper

Swapper provides an API for Django's…

permissive · top 15,000 on PyPI

drf-extra-fields

Provides specialized serializer fields for…

permissive · top 15,000 on PyPI

drf-dynamic-fields

Provides a Django REST Framework mixin that…

permissive · top 15,000 on PyPI

djangorestframework-dataclasses

Automatically generates Django REST Framework…

permissive · top 5,000 on PyPI

djangorestframework-bulk

Adds bulk create, update, and delete operations…

permissive · top 15,000 on PyPI

drf-nested-routers

Provides routers and serializer fields for…

permissive · top 5,000 on PyPI