I'm trying to figure out how to save related models using Django REST framework.
In my app I have a model Recipe
with 2 related models: RecipeIngredient
and RecipeStep
. A Recipe
object MUST have at least 3 related RecipeIngredient
and 3 RecipeStep
. Before the introduction of the REST framework I was using a Django CreateView
with two formsets and the save process was the following (follow the code from form_valid()
):
def save_formsets(self, recipe):
for f in self.get_formsets():
f.instance = recipe
f.save()
def save(self, form):
with transaction.atomic():
recipe = form.save()
self.save_formsets(recipe)
return recipe
def formsets_are_valid(self):
return all(f.is_valid() for f in self.get_formsets())
def form_valid(self, form):
try:
if self.formsets_are_valid():
try:
return self.create_ajax_success_response(form)
except IntegrityError as ie:
return self.create_ajax_error_response(form, {'IntegrityError': ie.message})
except ValidationError as ve:
return self.create_ajax_error_response(form, {'ValidationError': ve.message})
return self.create_ajax_error_response(form)
Now I have my RecipeViewSet
:
class RecipeViewSet(ModelViewSet):
serializer_class = RecipeSerializer
queryset = Recipe.objects.all()
permission_classes = (RecipeModelPermission, )
which uses RecipeSerializer
:
class RecipeSerializer(serializers.ModelSerializer):
class Meta:
model = Recipe
fields = (
'name', 'dish_type', 'cooking_time', 'steps', 'ingredients'
)
ingredients = RecipeIngredientSerializer(many=True)
steps = RecipeStepSerializer(many=True)
and these are the related serializers:
class RecipeIngredientSerializer(serializers.ModelSerializer):
class Meta:
model = RecipeIngredient
fields = ('name', 'quantity', 'unit_of_measure')
class RecipeStepSerializer(serializers.ModelSerializer):
class Meta:
model = RecipeStep
fields = ('description', 'photo')
Now... how I'm supposed to validate related models (RecipeIngredient
and RecipeStep
) and save them when RecipeViewSet
's create()
method is called? (is_valid()
in RecipeSerializer
is actually ignoring nested relationships and reporting only errors related to the main model Recipe
).
At the moment I tried to override the is_valid()
method in RecipeSerializer
, but is not so simple... any idea?