D
D
Dmitry Shapoval2015-07-26 12:22:25
Django
Dmitry Shapoval, 2015-07-26 12:22:25

How to properly save/load models with dependencies?

emberjs frontend, django+django-rest-framework backend
has an order form. the user there selects values ​​from which value stretches from other tables.
on the server side, of course, the order is presented as a model and the associated tables are models.ForeignKey there, everything is clear with this.
when reading the model from the server (list, retrieve), it seems logical to me to give the model immediately with the inclusion of dependent data (EMBEDDED RECORDS in terms of emberjs). again, there are no problems with this, drf can give it like that.

{
"mainmodel":
{
"id": 1,
"simplefield": "string",
"complexfield" : {
 "id": 1,
 "name": "somename"
 }
}
}

but when creating a new record, it seems not good to include the value of dependent data. and drf swears that he doesn’t know how out of the box and you need to create a custom create, and it will be considered that you want to create the main model and the dependent one right away. in general, when creating, id is sent to the data of dependent tables.
{
"mainmodel":
{
"simplefield": "string2",
"complexfield" :  2
}
}

the trouble is that drf, after successfully creating a record, returns the same json to the ember, and the ember, as I understand it, expects a model with embedded records in response even to a request to create a model. and as a result, the model is correctly created on the server, and the ember returns an error, because it received something that it did not expect.
Did I choose the right yucase at all? and how to work with models with dependencies in the emberjs+django bundle?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Romanov, 2015-07-26
@Kaer_Morchen

C django-rest-framework did not work, but I see several possible solutions.
Give the models like this:

{
    "mainmodel": {
        "id": 1,
        "simplefield": "string",
        "complexfield": 2
    },
    "complexfield": {
       "id": 1,
        "name": "somename"
    }
}

In the Ember model, respectively:
Or try using some ember-django-adapter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question