Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to make circular imports for included fields#1123

Answeredbysliverc
Nekidev asked this question inQ&A
Discussion options

I have the following serializers:

# Module 1fromartist.serializersimportArtistSerializerclassImageSerializer(serializers.ModelSerializer):included_serializers= {"artist":ArtistSerializer    }classMeta:model=Imagefields= ["artist"]# Module 2fromimage.serializersimportImageSerializerclassArtistSerializer(serializers.ModelSerializer):included_serializers= {"images":ImageSerializer    }classMeta:model=Artistfields= ["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?

You must be logged in to vote

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:

classImageSerializer(serializers.ModelSerializer):included_serializers= {"artist":"artist.serializers.ArtistSerializer"    }classMeta:model=Imagefields= ["artist"]

Defining the serializers as dotted import string should resolve your circular import problem.

Replies: 1 comment 1 reply

Comment options

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:

classImageSerializer(serializers.ModelSerializer):included_serializers= {"artist":"artist.serializers.ArtistSerializer"    }classMeta:model=Imagefields= ["artist"]

Defining the serializers as dotted import string should resolve your circular import problem.

You must be logged in to vote
1 reply
@Nekidev
Comment options

Thanks, I had tried using a string but without theserializers module since models do not require it.

Answer selected byNekidev
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@Nekidev@sliverc

[8]ページ先頭

©2009-2025 Movatter.jp