0

nested serializer showing null data

from rest_framework import serializers
from .models import PlayerTable, ChildTable

class ChildTableSerializer(serializers.ModelSerializer):
    # x= ChildTable.objects.all().values
    class Meta:
        model = ChildTable
        fields = ('season','goals','fk')
        # fields =('fk',)

class PlayerTableSerializer(serializers.ModelSerializer):
    player_details = ChildTableSerializer(many=True, read_only=True)

    class Meta:
        model = PlayerTable
        fields =  ('player_details',)

please help data getting by serializer is null

  • class ChildTable(models.Model): season = models.IntegerField(blank=True, null=True) goals = models.IntegerField(blank=True, null=True) fk = models.ForeignKey('PlayerTable', models.CASCADE, blank=True, null=True) cid = models.AutoField(primary_key=True) class Meta: # managed = False db_table = 'child_table' class PlayerTable(models.Model): player_id = models.AutoField(primary_key=True) player_name = models.CharField(unique=True, max_length=45) class Meta: # managed = False db_table = 'player_table' – Saurabh Gahlot May 20 '19 at 12:35
  • 1
    Please add more details in your question. What is being null / which line it shows the null and also update the question with errors you are getting. – smilyface May 20 '19 at 12:36
  • And post the relevant code into the question please. – Sfili_81 May 20 '19 at 12:38
  • https://stackoverflow.com/questions/52823567/eofexception-calling-a-rest-authentication-controller Take this as an example. See how this is formatted with full details. – smilyface May 20 '19 at 12:39
  • @SaurabhGahlot show your model of PlayerTable, ChildTable – rahul.m May 20 '19 at 12:54

1 Answers1

0

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.

dirkgroten
  • 20,112
  • 2
  • 29
  • 42
  • please add reference links (like why use `childtable_set` if there is no related field) to make this answer more complete. – Sushant May 20 '19 at 17:40