what is the field 'player-details'
? It's not a field on your PlayerTable
model. You need to use the name of the related field. In your case since you have a ForeignKey
relationship ChildTable
-->
PlayerTable
and you haven't specified the related_name
, it's childtable_set
. So if you do this it should work:
class PlayerTableSerializer(serializers.ModelSerializer):
childtable_set = ChildTableSerializer(many=True, read_only=True)
class Meta:
model = PlayerTable
fields = ('childtable_set',)
Alternatively, change your models naming to be more aligned with Django conventions:
class PlayerDetail(models.Model):
player = models.ForeignKey(Player, db_column="fk", related_name="player_details", null=True, blank=True, on_delete=models.CASCADE)
...
class Meta:
managed = False
db_table = "child_table"
class Player(models.Model):
name = models.CharField(db_column="player_name", ...)
class Meta:
db_table = "player_table"
then your serializer would work because the relation is player_details
. This also has the advantage that when you do details.player
you get the player object (now, you have to do details.fk
but that actually doesn't return the foreign key value, it returns the Player
object). Also your models have more pythonic names (Player
not PlayerTable
). Your code will be much more readable.