I'm using Django REST Framework
.
I have 3 different models, 2 are standalone models while the 3rd one is a bridge model
that connects the first 2 models
# First standalone model
class Build(TimeStampedModel):
name = models.CharField(max_length=64, null=False, blank=False)
type = models.CharField(max_length=64, null=False, blank=False)
play_as = models.CharField(max_length=64, null=False, blank=False)
link = models.CharField(max_length=255, null=False, blank=False)
class Meta:
ordering = ['-created_at']
def __str__(self):
return f'{self.play_as} - {self.name}'
# Second standalone model
class Run(TimeStampedModel):
name = models.CharField(max_length=64, null=False, blank=False)
difficulty = models.CharField(max_length=64, null=False, blank=False)
origin = models.CharField(max_length=64, null=False, blank=False)
alignment = models.CharField(max_length=64, null=False, blank=False)
class Meta:
ordering = ['-created_at']
def __str__(self):
return f'{self.name} - {self.difficulty}'
# Bridge model
class RunBuild(TimeStampedModel):
run = models.ForeignKey(
Run, related_name='runBuild_run', on_delete=models.CASCADE, null=False, blank=False
)
build = models.ForeignKey(
Build, related_name='runBuild_builds', on_delete=models.CASCADE, null=False, blank=False
)
class Meta:
ordering = ['-run']
def __str__(self):
return f'{self.run.name} - {self.build.name}'
The Build
serializer is fine, the problem stands in RunBuild
and Run
serializer.
# Model Serializer for Build model
class BuildSerializer(serializers.ModelSerializer):
created_at = serializers.SerializerMethodField()
class Meta:
model = Build
exclude = ['updated_at']
def get_created_at(self, instance):
return instance.created_at.strftime('%B %d, %Y')
# Model Serializer for RunBuild model
class RunBuildSerializer(serializers.ModelSerializer):
# This doesn't work
runBuild_builds = BuildSerializer(many=True, read_only=True)
created_at = serializers.SerializerMethodField()
class Meta:
model = RunBuild
fields = ['run', 'build', 'created_at', 'runBuild_builds']
def get_created_at(self, instance):
return instance.created_at.strftime('%B %d, %Y')
# Model Serializer for Run model
class RunSerializer(serializers.ModelSerializer):
# This works
runBuild_run = RunBuildSerializer(many=True, read_only=True)
created_at = serializers.SerializerMethodField()
class Meta:
model = Run
fields = [
'name',
'difficulty',
'origin',
'alignment',
'created_at',
'runBuild_run'
]
def get_created_at(self, instance):
# We will format the date
return instance.created_at.strftime('%B %d, %Y')
A Run
is composed by few fields and many Builds
under it, that's why I needed a bridge model. I'm able to show many RunBuild
in the Run
serializer, like this
{
"name": "Versatile Bard",
"difficulty": "Tactician",
"origin": "The Dark Urge",
"alignment": "Chaotic Neutral",
"created_at": "March 14, 2024",
"runBuild_run": [
{
"run": 1,
"build": 1,
"created_at": "March 14, 2024"
},
{
"run": 1,
"build": 2,
"created_at": "March 14, 2024"
},
....
]
}
But if I do the same thing for RunBuild
serializer, it won't show a thing (and I want it to show each Build
on it)
{
"run": 1,
"build": 1,
"created_at": "March 14, 2024"
"runBuild_builds": [
{
"name": "Cleric / Bard - CC / Versatile Spellcaster",
"type": "Main Character",
"play_as": "Main Character",
"link": "......."
},
....
]
}
The runBuild_builds
field is not present in the answer, I added by hand now to show you how it should be.
The ultimate result should be, in Run
serializer, having a result like this
{
"name": "Versatile Bard",
"difficulty": "Tactician",
"origin": "The Dark Urge",
"alignment": "Chaotic Neutral",
"created_at": "March 14, 2024",
"runBuild_run": [
{
"run": 1,
"build": 1,
"created_at": "March 14, 2024",
"runBuild_builds": [
{
"name": "Cleric / Bard - CC / Versatile Spellcaster",
"type": "Main Character",
"play_as": "Main Character",
"link": "......."
},
....
]
},
....
]
}
Any idea on how to modify my code?
Update your RunBuildSerializer
like this,
class RunBuildSerializer(serializers.ModelSerializer):
# This doesn't work
build = BuildSerializer(many=True, read_only=True)
created_at = serializers.SerializerMethodField()
class Meta:
model = RunBuild
fields = ['run', 'build', 'created_at']
def get_created_at(self, instance):
return instance.created_at.strftime('%B %d, %Y')
Remove the runBuild_builds
fields from this serializer. Because this is related_name field it needs to use when we need to access RunBuild
class from Build
class.