Serializing Multiple API Fields into one. Django












0















I have a pre-defined API, like:



{
time : some_time,
height : {1: 154, 2: 300, 3: 24},
color : {1: 'red', 2: 'blue', 3: 'green'},
age : {1: 27, 2: 324, 3: 1},
... many, many more keys.
}


I have no control of this API, so cannot change its structure.



Each integer key inside the sub dictionaries are linked and part of one record. For example the object that is 154 in height, is also colour: red and age: 27.



I am aware one strategy to work with this is to have separate serialisers for each field.



class MySerializer(serializers.ModelSerializer):
# Nested serializers
height = HeightSerializer()
colour = ColourSerializer()
age = AgeSerializer()
etc, etc, etc


But that still gives me messy data to work with, that requires lots of update() logic in the serializer.



What I instead want to do is have one nested serializer that has access to the full request data, and can work with height, colour and age simultaneously and return me something like from the to_internal_value() method:



{ 
['record' : 1, 'height': 154, 'colour' : 'red', 'age' : 27],
['record' : 2, 'height': 300, 'colour' : 'blue', 'age' : 324],
['record' : 3, 'height': 24, 'colour' : 'green', 'age' : 2],
}


But unfortunately the height serializer only seems to have access to information on fields called height. I am aware I can user source="foo" in the init call, but then it only has access to a field called "foo". I want it to have access to all fields.



I noticed there is a source='*' option, but it doesn't work. My init method of the serializer never gets called unless there is a key "height" in the api call.



Any ideas how I can have a nested serialiser that has access to all the data in the request?



Thanks



Joey










share|improve this question























  • Is this really a ModelSerializer? Where does the model come in?

    – Daniel Roseman
    Nov 26 '18 at 14:43











  • There's a database table with fields like: record_id, time, height, colour, age. I feel like I need to transform the badly designed input API, into something very similar to a record structure, so I can make use of the built in update logic in the model serialiser. The objective is to save these records in this table.

    – Joey O
    Nov 26 '18 at 14:46













  • It sounds like you should leave the fields as they are and instead override to_internal_value on MySerializer itself.

    – Daniel Roseman
    Nov 26 '18 at 14:56
















0















I have a pre-defined API, like:



{
time : some_time,
height : {1: 154, 2: 300, 3: 24},
color : {1: 'red', 2: 'blue', 3: 'green'},
age : {1: 27, 2: 324, 3: 1},
... many, many more keys.
}


I have no control of this API, so cannot change its structure.



Each integer key inside the sub dictionaries are linked and part of one record. For example the object that is 154 in height, is also colour: red and age: 27.



I am aware one strategy to work with this is to have separate serialisers for each field.



class MySerializer(serializers.ModelSerializer):
# Nested serializers
height = HeightSerializer()
colour = ColourSerializer()
age = AgeSerializer()
etc, etc, etc


But that still gives me messy data to work with, that requires lots of update() logic in the serializer.



What I instead want to do is have one nested serializer that has access to the full request data, and can work with height, colour and age simultaneously and return me something like from the to_internal_value() method:



{ 
['record' : 1, 'height': 154, 'colour' : 'red', 'age' : 27],
['record' : 2, 'height': 300, 'colour' : 'blue', 'age' : 324],
['record' : 3, 'height': 24, 'colour' : 'green', 'age' : 2],
}


But unfortunately the height serializer only seems to have access to information on fields called height. I am aware I can user source="foo" in the init call, but then it only has access to a field called "foo". I want it to have access to all fields.



I noticed there is a source='*' option, but it doesn't work. My init method of the serializer never gets called unless there is a key "height" in the api call.



Any ideas how I can have a nested serialiser that has access to all the data in the request?



Thanks



Joey










share|improve this question























  • Is this really a ModelSerializer? Where does the model come in?

    – Daniel Roseman
    Nov 26 '18 at 14:43











  • There's a database table with fields like: record_id, time, height, colour, age. I feel like I need to transform the badly designed input API, into something very similar to a record structure, so I can make use of the built in update logic in the model serialiser. The objective is to save these records in this table.

    – Joey O
    Nov 26 '18 at 14:46













  • It sounds like you should leave the fields as they are and instead override to_internal_value on MySerializer itself.

    – Daniel Roseman
    Nov 26 '18 at 14:56














0












0








0








I have a pre-defined API, like:



{
time : some_time,
height : {1: 154, 2: 300, 3: 24},
color : {1: 'red', 2: 'blue', 3: 'green'},
age : {1: 27, 2: 324, 3: 1},
... many, many more keys.
}


I have no control of this API, so cannot change its structure.



Each integer key inside the sub dictionaries are linked and part of one record. For example the object that is 154 in height, is also colour: red and age: 27.



I am aware one strategy to work with this is to have separate serialisers for each field.



class MySerializer(serializers.ModelSerializer):
# Nested serializers
height = HeightSerializer()
colour = ColourSerializer()
age = AgeSerializer()
etc, etc, etc


But that still gives me messy data to work with, that requires lots of update() logic in the serializer.



What I instead want to do is have one nested serializer that has access to the full request data, and can work with height, colour and age simultaneously and return me something like from the to_internal_value() method:



{ 
['record' : 1, 'height': 154, 'colour' : 'red', 'age' : 27],
['record' : 2, 'height': 300, 'colour' : 'blue', 'age' : 324],
['record' : 3, 'height': 24, 'colour' : 'green', 'age' : 2],
}


But unfortunately the height serializer only seems to have access to information on fields called height. I am aware I can user source="foo" in the init call, but then it only has access to a field called "foo". I want it to have access to all fields.



I noticed there is a source='*' option, but it doesn't work. My init method of the serializer never gets called unless there is a key "height" in the api call.



Any ideas how I can have a nested serialiser that has access to all the data in the request?



Thanks



Joey










share|improve this question














I have a pre-defined API, like:



{
time : some_time,
height : {1: 154, 2: 300, 3: 24},
color : {1: 'red', 2: 'blue', 3: 'green'},
age : {1: 27, 2: 324, 3: 1},
... many, many more keys.
}


I have no control of this API, so cannot change its structure.



Each integer key inside the sub dictionaries are linked and part of one record. For example the object that is 154 in height, is also colour: red and age: 27.



I am aware one strategy to work with this is to have separate serialisers for each field.



class MySerializer(serializers.ModelSerializer):
# Nested serializers
height = HeightSerializer()
colour = ColourSerializer()
age = AgeSerializer()
etc, etc, etc


But that still gives me messy data to work with, that requires lots of update() logic in the serializer.



What I instead want to do is have one nested serializer that has access to the full request data, and can work with height, colour and age simultaneously and return me something like from the to_internal_value() method:



{ 
['record' : 1, 'height': 154, 'colour' : 'red', 'age' : 27],
['record' : 2, 'height': 300, 'colour' : 'blue', 'age' : 324],
['record' : 3, 'height': 24, 'colour' : 'green', 'age' : 2],
}


But unfortunately the height serializer only seems to have access to information on fields called height. I am aware I can user source="foo" in the init call, but then it only has access to a field called "foo". I want it to have access to all fields.



I noticed there is a source='*' option, but it doesn't work. My init method of the serializer never gets called unless there is a key "height" in the api call.



Any ideas how I can have a nested serialiser that has access to all the data in the request?



Thanks



Joey







json django api model serializer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 14:39









Joey OJoey O

286




286













  • Is this really a ModelSerializer? Where does the model come in?

    – Daniel Roseman
    Nov 26 '18 at 14:43











  • There's a database table with fields like: record_id, time, height, colour, age. I feel like I need to transform the badly designed input API, into something very similar to a record structure, so I can make use of the built in update logic in the model serialiser. The objective is to save these records in this table.

    – Joey O
    Nov 26 '18 at 14:46













  • It sounds like you should leave the fields as they are and instead override to_internal_value on MySerializer itself.

    – Daniel Roseman
    Nov 26 '18 at 14:56



















  • Is this really a ModelSerializer? Where does the model come in?

    – Daniel Roseman
    Nov 26 '18 at 14:43











  • There's a database table with fields like: record_id, time, height, colour, age. I feel like I need to transform the badly designed input API, into something very similar to a record structure, so I can make use of the built in update logic in the model serialiser. The objective is to save these records in this table.

    – Joey O
    Nov 26 '18 at 14:46













  • It sounds like you should leave the fields as they are and instead override to_internal_value on MySerializer itself.

    – Daniel Roseman
    Nov 26 '18 at 14:56

















Is this really a ModelSerializer? Where does the model come in?

– Daniel Roseman
Nov 26 '18 at 14:43





Is this really a ModelSerializer? Where does the model come in?

– Daniel Roseman
Nov 26 '18 at 14:43













There's a database table with fields like: record_id, time, height, colour, age. I feel like I need to transform the badly designed input API, into something very similar to a record structure, so I can make use of the built in update logic in the model serialiser. The objective is to save these records in this table.

– Joey O
Nov 26 '18 at 14:46







There's a database table with fields like: record_id, time, height, colour, age. I feel like I need to transform the badly designed input API, into something very similar to a record structure, so I can make use of the built in update logic in the model serialiser. The objective is to save these records in this table.

– Joey O
Nov 26 '18 at 14:46















It sounds like you should leave the fields as they are and instead override to_internal_value on MySerializer itself.

– Daniel Roseman
Nov 26 '18 at 14:56





It sounds like you should leave the fields as they are and instead override to_internal_value on MySerializer itself.

– Daniel Roseman
Nov 26 '18 at 14:56












0






active

oldest

votes











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/3.0/"u003ecc by-sa 3.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%2f53483452%2fserializing-multiple-api-fields-into-one-django%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53483452%2fserializing-multiple-api-fields-into-one-django%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)