-
Notifications
You must be signed in to change notification settings - Fork 300
-
I have the following serializers:
# Module 1 from artist.serializers import ArtistSerializer class ImageSerializer(serializers.ModelSerializer): included_serializers = { "artist": ArtistSerializer } class Meta: model = Image fields = ["artist"] # Module 2 from image.serializers import ImageSerializer class ArtistSerializer(serializers.ModelSerializer): included_serializers = { "images": ImageSerializer } class Meta: model = Artist fields = ["images"]
The images field is a reverse relationship.
This raises a circular dependendy error. I have already tried importing the serializers from inside of the class, but this does not work because when one class is imported, the circular dependency problem starts again. Is there any way that this can be done without importing the serializers to each module? I cannot move the 2 serializers to the same module to fix the error since they need to be in their own apps. Is this impossible?
Beta Was this translation helpful? Give feedback.
All reactions
Hi @Nekidev. This is a good question. Django REST framework JSON:API supports dotted import string to define included serializers.
This could look something likes this:
class ImageSerializer(serializers.ModelSerializer): included_serializers = { "artist": "artist.serializers.ArtistSerializer" } class Meta: model = Image fields = ["artist"]
Defining the serializers as dotted import string should resolve your circular import problem.
Replies: 1 comment 1 reply
-
Hi @Nekidev. This is a good question. Django REST framework JSON:API supports dotted import string to define included serializers.
This could look something likes this:
class ImageSerializer(serializers.ModelSerializer): included_serializers = { "artist": "artist.serializers.ArtistSerializer" } class Meta: model = Image fields = ["artist"]
Defining the serializers as dotted import string should resolve your circular import problem.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
Thanks, I had tried using a string but without the serializers module since models do not require it.
Beta Was this translation helpful? Give feedback.