Registry / web-framework / rest-framework-generic-relations

rest-framework-generic-relations

JSON →
library2.2.0pypypiunverified

A Django REST Framework extension providing a GenericRelatedField for serializers to handle generic foreign keys (GFKs). Version 2.2.0 supports Python >=3.8 and DRF >=3.12. Stable, infrequent releases.

pip install rest-framework-generic-relations
INSTALL
IMPORT
SIG · REST-FRAMEWORK-GEN
R
rest-framework-generic-relations
web-frameworkpythonv2.2.0
Install
3.7s avg
Import
Disk
70MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 70.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.7s · import 0.000s · 71MB
70MB installed
● package 70MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GenericRelatedField
from generic_relations.fields import GenericRelatedField
from rest_framework_generic_relations.fields import GenericRelatedField

Define a serializer with a GenericRelatedField that provides different serializers based on the GFK content type.

from rest_framework_generic_relations.fields import GenericRelatedField from rest_framework import serializers from myapp.models import Tag, Product, Article class TaggedItemSerializer(serializers.ModelSerializer): content_object = GenericRelatedField( serializer_polymorphic=lambda: { Product: ProductSerializer, Article: ArticleSerializer, } ) class Meta: model = Tag fields = ['id', 'tag', 'content_object']
Debug
Known issues
gotchaGenericRelatedField requires a serializer_polymorphic mapping or custom serializer_class; omitting either will raise an AttributeError.
fix
Provide a dictionary mapping ContentType model classes to serializers via serializer_polymorphic.
affects: all
gotchaThe field expects the GFK to be properly set up on the model with a GenericForeignKey; otherwise it will fail to resolve.
fix
Ensure the model has a GenericForeignKey field pointing to the content type and object id fields.
affects: all
deprecatedIn version 2.x, the serializer_class parameter is deprecated in favor of serializer_polymorphic.
fix
Use serializer_polymorphic instead to specify serializers per model.
affects: >=2.0.0
Upgrade
Version history
2.2.0latest on PyPI · released Mar 27, 2024
Audit
Dependencies
djangorequiredRequired dependency for Django models and GFK
djangorestframeworkrequiredRequired for DRF serializers and views
Agent activity
16 hits · last 30 days
node
14
Meta
1
Resources
rest-framework-generic-relations — pip install rest-framework-generic-relations · libregistry