title: Django Rest Framework writable nested serializers
body: <p>I'm writing a recipe organizer as a sample project for a class. I'm not very experienced with DRF other than using some very basic functionality. Here's the objective:</p>
  
  <p>Create a new Recipe with associated Ingredients. Create the Ingredient objects at the same time as creating the Recipe object.</p>
  
  <p><strong>models.py:</strong></p>
  
  <pre><code>class Ingredient(models.Model):
      name = models.CharField(max_length=100)
  
      def __str__(self):
          return self.name
  
  
  class Recipe(models.Model):
      name = models.CharField(max_length=100)
      description = models.TextField(blank=True, null=True, help_text="This is a quick description of your recipe")
      directions = models.TextField(help_text="How to make the recipe")
      ingredients = models.ManyToManyField(Ingredient)
  
      def __str__(self):
          return self.name
  </code></pre>
  
  <p><br>
  <strong>serializers.py</strong></p>
  
  <pre><code>class IngredientSerializer(serializers.ModelSerializer):
  
      class Meta:
          model = Ingredient
  
  
  class RecipeSerializer(serializers.ModelSerializer):
      ingredients = IngredientSerializer(many=True)
  
      class Meta:
          model = Recipe
  
      def create(self, validated_data):
          ingredients_data = validated_data.pop('ingredients')
          recipe = Recipe.objects.create(**validated_data)
          for ingredient_data in ingredients_data:
              Ingredient.objects.create(**ingredient_data)
          return recipe
  </code></pre>
  
  <p>This successfully creates the Recipe object AND the Ingredients objects in the database, but doesn't associate the list of Ingredients with the Recipe. I assume this is because when I run <code>ingredients_data = validated_data.pop('ingredients')</code>, the <code>validated_data</code> dictionary gets its Ingredients removed, so when I create a new Recipe using <code>validated_data</code>, there aren't associated ingredients.</p>
  
  <p>However I can't seem to figure out a way to keep ingredients associated with the recipe.</p>