ShawnCramp / intellileague

0 stars 0 forks source link

Nested Item Creation #3

Closed ShawnCramp closed 8 years ago

ShawnCramp commented 8 years ago

Having a minor problem that I'm not sure if you would be able to see an answer too. But since we are going to do nested created between row and item. I have JSON that looks like this for the POST call:

{ "build": 1, "title": "Heavy AP", "item_row": [ { "row": null, "item": 3102 }, { "row": null, "item": 3065 } ] }

The way it works if that I pop "item_row" off the JSON object and create the row item first, being:

{ "build" 1, "title": "Heavy AP" }

Then I loop through all the items from "item_row" and create them afterwards. But, "row" is a required field on the JSON object, but I have no idea what I should set it to, in order to make sure I attach it to the row object Im creating first.

The Python nested create looks like this:

def create(self, validated_data): items_data = validated_data.pop('item_row') row = Row.objects.create(_validated_data) for item_data in items_data: Item.objects.create(row=row, _item_data) return row

Since Im actually setting row of each item object to the row object I just created, then technically the row item in the JSON shouldn't matter since it gets overwritten anyways. But I'm not sure if I should just set row to be some random valid number, since its overwritten or what I should do here lol

ShawnCramp commented 8 years ago

Nvm rofl I fixed it. Nested creation works

xandros-na commented 8 years ago

:+1: