{"id":24524,"library":"rest-framework-generic-relations","title":"Django REST Framework Generic Relations","description":"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.","status":"active","version":"2.2.0","language":"python","source_language":"en","source_url":"https://github.com/craigds/rest-framework-generic-relations","tags":["django","django-rest-framework","generic-relations","gfk","serializers"],"install":[{"cmd":"pip install rest-framework-generic-relations","lang":"bash","label":"Install via pip"}],"dependencies":[{"reason":"Required dependency for Django models and GFK","package":"django","optional":false},{"reason":"Required for DRF serializers and views","package":"djangorestframework","optional":false}],"imports":[{"note":"Module is nested under fields","wrong":"from rest_framework_generic_relations import GenericRelatedField","symbol":"GenericRelatedField","correct":"from rest_framework_generic_relations.fields import GenericRelatedField"}],"quickstart":{"code":"from rest_framework_generic_relations.fields import GenericRelatedField\nfrom rest_framework import serializers\nfrom myapp.models import Tag, Product, Article\n\nclass TaggedItemSerializer(serializers.ModelSerializer):\n    content_object = GenericRelatedField(\n        serializer_polymorphic=lambda: {\n            Product: ProductSerializer,\n            Article: ArticleSerializer,\n        }\n    )\n    class Meta:\n        model = Tag\n        fields = ['id', 'tag', 'content_object']","lang":"python","description":"Define a serializer with a GenericRelatedField that provides different serializers based on the GFK content type."},"warnings":[{"fix":"Provide a dictionary mapping ContentType model classes to serializers via serializer_polymorphic.","message":"GenericRelatedField requires a serializer_polymorphic mapping or custom serializer_class; omitting either will raise an AttributeError.","severity":"gotcha","affected_versions":"all"},{"fix":"Ensure the model has a GenericForeignKey field pointing to the content type and object id fields.","message":"The field expects the GFK to be properly set up on the model with a GenericForeignKey; otherwise it will fail to resolve.","severity":"gotcha","affected_versions":"all"},{"fix":"Use serializer_polymorphic instead to specify serializers per model.","message":"In version 2.x, the serializer_class parameter is deprecated in favor of serializer_polymorphic.","severity":"deprecated","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Use 'from rest_framework_generic_relations.fields import GenericRelatedField'","cause":"Importing from the top-level module instead of the fields submodule.","error":"AttributeError: module 'rest_framework_generic_relations' has no attribute 'GenericRelatedField'"},{"fix":"Provide a valid serializer_polymorphic dict mapping models to serializers.","cause":"serializer_polymorphic or serializer_class not set, or serializer_polymorphic returns None.","error":"TypeError: 'NoneType' object is not callable"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}