Trying to create nested view of django model classes using relationsdjango - inlineformset_factory with more than one ForeignKeyHow do I properly nest serializers in Django REST Framework?Django REST Framework create/update 2 nested ForeignKeysSerializer as Field is not visible in jsonDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerHow to expose some specific fields of model_b based on a field of model_a?related name in parent model in django if inherited in other modelHow to define Mode with generic ForeignKey in DjangoHow to remove Django redundant inner join

What are the advantages and disadvantages of tail wheels that cause modern airplanes to not use them?

What is this gigantic dish at Ben Gurion airport?

Is there any reason to concentrate on the Thunderous Smite spell after using its effects?

Are there any rules about taking damage whilst holding your breath in combat?

Insight into cavity resonators

What is the meaning of 「ぞんぞん」?

Bit one of the Intel 8080's Flags register

What was the ultimate objective of The Party in 1984?

Is the Dodge action perceptible to other characters?

How to draw a Venn diagram for X - (Y intersect Z)?

What explanation do proponents of a Scotland-NI bridge give for it breaking Brexit impasse?

Is it possible to format a USB from a live USB?

Kitchen Sink Plumbing

Has SHA256 been broken by Treadwell Stanton DuPont?

Building Truncatable Primes using Nest(List), While, Fold

What is a "major country" as named in Bernie Sanders' Healthcare debate answers?

How to write characters doing illogical things in a believable way?

Block diagram vs flow chart?

Can a character with good/neutral alignment attune to a sentient magic item with evil alignment?

What organs or modifications would be needed for a life biological creature not to require sleep?

shell script to check if input is a string/integer/float

Meaning of Swimming their horses

'Overwrote' files, space still occupied, are they lost?

Shouldn't countries like Russia and Canada support global warming?



Trying to create nested view of django model classes using relations


django - inlineformset_factory with more than one ForeignKeyHow do I properly nest serializers in Django REST Framework?Django REST Framework create/update 2 nested ForeignKeysSerializer as Field is not visible in jsonDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerHow to expose some specific fields of model_b based on a field of model_a?related name in parent model in django if inherited in other modelHow to define Mode with generic ForeignKey in DjangoHow to remove Django redundant inner join






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I am trying to follow this documentation to create nested serializer and api view.
https://www.django-rest-framework.org/api-guide/relations/#nested-relationships



However, I am not able to understand what did I miss as my results are not expected.



I have followed this example to my case and checked various other guides regarding same. Tried different views and different serializer formats.



Code for model is this:-



class Round(models.Model):
player_num = models.IntegerField(null=False, default=1)

class Seats(models.Model):
stack = models.IntegerField(null=False, default=0)
round = models.ForeignKey(Round, on_delete=models.CASCADE)
state = models.IntegerField(choices=STATE)



code for Serializers is this:-



class SeatsSerializer(serializers.ModelSerializer):
class Meta:
model = Seats
fields = ('stack','state')

class RoundSerializer(serializers.ModelSerializer):
seats = SeatsSerializer(many = True, read_only=True)

class Meta:
model = Round
fields = ('player_num','seats')


I want output like this:




'player_num': 3,
'seats': [
'stack': 100, 'state': 'participating', 'name': 'p1', 'uuid': 'ftwdqkystzsqwjrzvludgi',
'stack': 100, 'state': 'participating', 'name': 'p2', 'uuid': 'bbiuvgalrglojvmgggydyt',
]



However, output I get is:




'player_num': 3,










share|improve this question


























  • There needs to be a relationship between the Seats and Round models if you want to use DRF nested serializers.

    – Endre Both
    Mar 28 at 12:27












  • There is, I made a typo. I made the edit now. Please check

    – Himanshu Sharma
    Mar 28 at 12:30






  • 1





    Try replacing seats with seats_set in RoundSerializer (2x).

    – Endre Both
    Mar 28 at 12:39











  • Oh .. works!! Such lame error. Thanks!

    – Himanshu Sharma
    Mar 28 at 13:04






  • 1





    No problem. Consider using singular model names though (Seat).

    – Endre Both
    Mar 28 at 13:11

















1















I am trying to follow this documentation to create nested serializer and api view.
https://www.django-rest-framework.org/api-guide/relations/#nested-relationships



However, I am not able to understand what did I miss as my results are not expected.



I have followed this example to my case and checked various other guides regarding same. Tried different views and different serializer formats.



Code for model is this:-



class Round(models.Model):
player_num = models.IntegerField(null=False, default=1)

class Seats(models.Model):
stack = models.IntegerField(null=False, default=0)
round = models.ForeignKey(Round, on_delete=models.CASCADE)
state = models.IntegerField(choices=STATE)



code for Serializers is this:-



class SeatsSerializer(serializers.ModelSerializer):
class Meta:
model = Seats
fields = ('stack','state')

class RoundSerializer(serializers.ModelSerializer):
seats = SeatsSerializer(many = True, read_only=True)

class Meta:
model = Round
fields = ('player_num','seats')


I want output like this:




'player_num': 3,
'seats': [
'stack': 100, 'state': 'participating', 'name': 'p1', 'uuid': 'ftwdqkystzsqwjrzvludgi',
'stack': 100, 'state': 'participating', 'name': 'p2', 'uuid': 'bbiuvgalrglojvmgggydyt',
]



However, output I get is:




'player_num': 3,










share|improve this question


























  • There needs to be a relationship between the Seats and Round models if you want to use DRF nested serializers.

    – Endre Both
    Mar 28 at 12:27












  • There is, I made a typo. I made the edit now. Please check

    – Himanshu Sharma
    Mar 28 at 12:30






  • 1





    Try replacing seats with seats_set in RoundSerializer (2x).

    – Endre Both
    Mar 28 at 12:39











  • Oh .. works!! Such lame error. Thanks!

    – Himanshu Sharma
    Mar 28 at 13:04






  • 1





    No problem. Consider using singular model names though (Seat).

    – Endre Both
    Mar 28 at 13:11













1












1








1








I am trying to follow this documentation to create nested serializer and api view.
https://www.django-rest-framework.org/api-guide/relations/#nested-relationships



However, I am not able to understand what did I miss as my results are not expected.



I have followed this example to my case and checked various other guides regarding same. Tried different views and different serializer formats.



Code for model is this:-



class Round(models.Model):
player_num = models.IntegerField(null=False, default=1)

class Seats(models.Model):
stack = models.IntegerField(null=False, default=0)
round = models.ForeignKey(Round, on_delete=models.CASCADE)
state = models.IntegerField(choices=STATE)



code for Serializers is this:-



class SeatsSerializer(serializers.ModelSerializer):
class Meta:
model = Seats
fields = ('stack','state')

class RoundSerializer(serializers.ModelSerializer):
seats = SeatsSerializer(many = True, read_only=True)

class Meta:
model = Round
fields = ('player_num','seats')


I want output like this:




'player_num': 3,
'seats': [
'stack': 100, 'state': 'participating', 'name': 'p1', 'uuid': 'ftwdqkystzsqwjrzvludgi',
'stack': 100, 'state': 'participating', 'name': 'p2', 'uuid': 'bbiuvgalrglojvmgggydyt',
]



However, output I get is:




'player_num': 3,










share|improve this question
















I am trying to follow this documentation to create nested serializer and api view.
https://www.django-rest-framework.org/api-guide/relations/#nested-relationships



However, I am not able to understand what did I miss as my results are not expected.



I have followed this example to my case and checked various other guides regarding same. Tried different views and different serializer formats.



Code for model is this:-



class Round(models.Model):
player_num = models.IntegerField(null=False, default=1)

class Seats(models.Model):
stack = models.IntegerField(null=False, default=0)
round = models.ForeignKey(Round, on_delete=models.CASCADE)
state = models.IntegerField(choices=STATE)



code for Serializers is this:-



class SeatsSerializer(serializers.ModelSerializer):
class Meta:
model = Seats
fields = ('stack','state')

class RoundSerializer(serializers.ModelSerializer):
seats = SeatsSerializer(many = True, read_only=True)

class Meta:
model = Round
fields = ('player_num','seats')


I want output like this:




'player_num': 3,
'seats': [
'stack': 100, 'state': 'participating', 'name': 'p1', 'uuid': 'ftwdqkystzsqwjrzvludgi',
'stack': 100, 'state': 'participating', 'name': 'p2', 'uuid': 'bbiuvgalrglojvmgggydyt',
]



However, output I get is:




'player_num': 3,







django rest api django-rest-framework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 12:30







Himanshu Sharma

















asked Mar 28 at 12:19









Himanshu SharmaHimanshu Sharma

579 bronze badges




579 bronze badges















  • There needs to be a relationship between the Seats and Round models if you want to use DRF nested serializers.

    – Endre Both
    Mar 28 at 12:27












  • There is, I made a typo. I made the edit now. Please check

    – Himanshu Sharma
    Mar 28 at 12:30






  • 1





    Try replacing seats with seats_set in RoundSerializer (2x).

    – Endre Both
    Mar 28 at 12:39











  • Oh .. works!! Such lame error. Thanks!

    – Himanshu Sharma
    Mar 28 at 13:04






  • 1





    No problem. Consider using singular model names though (Seat).

    – Endre Both
    Mar 28 at 13:11

















  • There needs to be a relationship between the Seats and Round models if you want to use DRF nested serializers.

    – Endre Both
    Mar 28 at 12:27












  • There is, I made a typo. I made the edit now. Please check

    – Himanshu Sharma
    Mar 28 at 12:30






  • 1





    Try replacing seats with seats_set in RoundSerializer (2x).

    – Endre Both
    Mar 28 at 12:39











  • Oh .. works!! Such lame error. Thanks!

    – Himanshu Sharma
    Mar 28 at 13:04






  • 1





    No problem. Consider using singular model names though (Seat).

    – Endre Both
    Mar 28 at 13:11
















There needs to be a relationship between the Seats and Round models if you want to use DRF nested serializers.

– Endre Both
Mar 28 at 12:27






There needs to be a relationship between the Seats and Round models if you want to use DRF nested serializers.

– Endre Both
Mar 28 at 12:27














There is, I made a typo. I made the edit now. Please check

– Himanshu Sharma
Mar 28 at 12:30





There is, I made a typo. I made the edit now. Please check

– Himanshu Sharma
Mar 28 at 12:30




1




1





Try replacing seats with seats_set in RoundSerializer (2x).

– Endre Both
Mar 28 at 12:39





Try replacing seats with seats_set in RoundSerializer (2x).

– Endre Both
Mar 28 at 12:39













Oh .. works!! Such lame error. Thanks!

– Himanshu Sharma
Mar 28 at 13:04





Oh .. works!! Such lame error. Thanks!

– Himanshu Sharma
Mar 28 at 13:04




1




1





No problem. Consider using singular model names though (Seat).

– Endre Both
Mar 28 at 13:11





No problem. Consider using singular model names though (Seat).

– Endre Both
Mar 28 at 13:11












1 Answer
1






active

oldest

votes


















2
















Try this:



round = models.ForeignKey(Round, on_delete=models.CASCADE, related_name='seats')





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55397476%2ftrying-to-create-nested-view-of-django-model-classes-using-relations%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2
















    Try this:



    round = models.ForeignKey(Round, on_delete=models.CASCADE, related_name='seats')





    share|improve this answer





























      2
















      Try this:



      round = models.ForeignKey(Round, on_delete=models.CASCADE, related_name='seats')





      share|improve this answer



























        2














        2










        2









        Try this:



        round = models.ForeignKey(Round, on_delete=models.CASCADE, related_name='seats')





        share|improve this answer













        Try this:



        round = models.ForeignKey(Round, on_delete=models.CASCADE, related_name='seats')






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 28 at 19:58









        Hugo BrilhanteHugo Brilhante

        4453 silver badges11 bronze badges




        4453 silver badges11 bronze badges





















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.




















            draft saved

            draft discarded















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55397476%2ftrying-to-create-nested-view-of-django-model-classes-using-relations%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript