jeudi 25 décembre 2014

Saving two new related objects in one request

I have two models in Django:



class Thread(models.Model):
entity = models.ForeignKey(Entity, null=True, blank=True)
...

class ThreadMessage(models.Model):
thread = models.ForeignKey(Thread, related_name='messages')
author = models.ForeignKey(User)
...


Now a client wants to create a new thread with first message in it. It has first to do a POST /threads to create a new thread and find out its id and then do POST /messages passing the found id in thread field.


I am thinking if it's reasonable and possible to do all of this in one request from Ember like:



POST /messages
{"message": {"text": "text", ...},
"thread": {"entity": 1}}


And the response would be:



{"message": {"text": "text", "id": 5678, "thread": 1234, ...},
"thread": {"entitity": 1, "id": 1234, ...}}




Aucun commentaire:

Enregistrer un commentaire