-
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.
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.
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:
Defining the serializers as dotted import string should resolve your circular import problem.