What is best way to deserialize JSON into multiple models in django rest framework
I am quite new to django rest framework and I would like to discuss what is the best way to implement an API endpoints I am aiming to have.
I have following JSON example:
[
{
'top_lvl_key1': {
id: 1
'some_key1': "value"
'some_key2' : "value"
}
},
{
'top_lvl_key2': {
id: 1
'some_key1': "value"
'some_key2' : "value"
}
},
{
'top_lvl_key1': {
id: 2
'some_key1': "value"
'some_key3' : "value"
}
},
{
'top_lvl_key3': {
id: 1
'some_key1': "value"
'some_key2' : "value"
}
}
]
and I want to have 3 endpoints
[POST] /insert
inserting and parsing the entire JSON to store it into models
[GET] /detail/<top_lvl_key>/
Display all record for specfici top_lvl_key
[GET] /detail//<top_lvl_key>/id/
Display a record for specific top_lvl_key and specific id (only 1 record)
As you can see, I need to store each type of top_lvl_key to different model so I can retrieve for GET requests. However I am not sure how should I got about serializing the JSON, since the dictionaries of the top_lvl_keys can differ. For example top_lvl_key_1
can sometimes have some_key_1
and some_key_2
, but sometimes some_key_1
and some_key_2
. So far I have been implementing the models with blank=True, null=True
, but I am not sure this is best solution as there a lot of attributes which can be NULL now. Is there a better way to go about this?
Thanks a lot in advance