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;
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
add a comment
|
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
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 replacingseats
withseats_set
inRoundSerializer
(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
add a comment
|
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
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
django rest api django-rest-framework
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 replacingseats
withseats_set
inRoundSerializer
(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
add a comment
|
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 replacingseats
withseats_set
inRoundSerializer
(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
add a comment
|
1 Answer
1
active
oldest
votes
Try this:
round = models.ForeignKey(Round, on_delete=models.CASCADE, related_name='seats')
add a comment
|
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Try this:
round = models.ForeignKey(Round, on_delete=models.CASCADE, related_name='seats')
add a comment
|
Try this:
round = models.ForeignKey(Round, on_delete=models.CASCADE, related_name='seats')
add a comment
|
Try this:
round = models.ForeignKey(Round, on_delete=models.CASCADE, related_name='seats')
Try this:
round = models.ForeignKey(Round, on_delete=models.CASCADE, related_name='seats')
answered Mar 28 at 19:58
Hugo BrilhanteHugo Brilhante
4453 silver badges11 bronze badges
4453 silver badges11 bronze badges
add a comment
|
add a comment
|
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
withseats_set
inRoundSerializer
(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