Skip to content

Instantly share code, notes, and snippets.

@mhubig
Created June 3, 2017 16:41
Show Gist options
  • Save mhubig/26c4d90159a87dedd0338ca9c7debc13 to your computer and use it in GitHub Desktop.
Save mhubig/26c4d90159a87dedd0338ca9c7debc13 to your computer and use it in GitHub Desktop.
def create(self, validated_data):
# get the sections data
sections = validated_data.pop('sections', [])
user = self.context['request'].user
# Create the chapter instance
chapter = Chapter.objects.create(**validated_data)
# Create each section instance
for section in sections:
widgets = section.pop('widgets', [])
section.update({'chapter': chapter})
section = Section.objects.create(**section)
# Create each widget instance
for widget in widgets:
widget.update({'section': section, 'user': user})
widget = Widget.objects.create(**widget)
return chapter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment