I'm building a web app and trying to send Post data as FormData to a Django Rest Framework Serializer. In the request.data I see that all the Post data is there, however after validating and saving the serializer it seems like some of the data did not get passed into validated_data.
Views.py
@api_view(["GET","POST"])
def api_list(request):
if request.method=="GET":
data = Recipe.objects.all()
serializer = RecipeSerializer(data, many=True)
return Response(serializer.data)
elif request.method=="POST":
print("POST recieved")
print (request.data) <----See below
serializer = RecipeSerializer(data=request.data)
print("Validating..")
if serializer.is_valid():
print("validated!")
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
print (serializer.errors)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
request.data
<QueryDict:
{'description': ['"gfdgdfg"'],
'name': ['"fdgdfgdf"'],
'ingredients': [
'{"name":"dfgdfg","amount":"gdfgd"}',
'{"name":"fdgdfg","amount":"dfgdf"}',
'{"name":"dfgdfgdf","amount":"gdfgdf"}'
],
'directions': [
'{"content":"gdfgfd"}',
'{"content":"gdfgdfg"}',
'{"content":"dfgdfdfg"}'
],
'image': [
<InMemoryUploadedFile: luisana-zerpa-MJPr6nOdppw-unsplash.jpg (image/jpeg)>
]
}>
serializer.py
class IngredientSerializer(serializers.ModelSerializer):
class Meta:
model = Ingredient
fields = ('name', 'amount')
class DirectionSerializer(serializers.ModelSerializer):
class Meta:
model = Direction
fields = ('content',)
class RecipeSerializer(serializers.ModelSerializer):
owner = serializers.StringRelatedField()
ingredients = IngredientSerializer(many=True, read_only=False)
directions = DirectionSerializer(many=True, read_only=False)
class Meta:
model = Recipe
fields = (
'id',
'name',
'owner',
'image',
'description',
'created_at',
'ingredients',
'directions',
)
def create(self, validated_data):
print (validated_data) <----See Below
has_ingredients = validated_data.get("ingredients")
has_directions = validated_data.get("directions")
if has_ingredients:
ingredient_data = validated_data.pop('ingredients')
if has_directions:
direction_data = validated_data.pop('directions')
recipe_name = validated_data.get('name')
recipe_name = recipe_name.replace('"','')
recipe_description = validated_data.get('description')
recipe_description = recipe_description.replace('"','')
recipe = Recipe.objects.create(name=recipe_name, description=recipe_description, image=validated_data.get('image'))
if has_ingredients:
for ingredient in ingredient_data:
Ingredient.objects.create(recipe=recipe, name=ingredient.get("name"), amount=ingredient.get("amount"))
if has_directions:
for direction in direction_data:
Direction.objects.create(recipe=recipe, content=direction.get("content"))
return recipe
validated_data NOTE: I can only get this if I add "required=False" for ingredient and directions else it just return a 404err
{
'name': '"fdgdfgdf"',
'image': <InMemoryUploadedFile: luisana-zerpa-MJPr6nOdppw-unsplash.jpg (image/jpeg)>,
'description': '"gfdgdfg"'
}
I tried looking into overiding the .is_valid() method on the serializers but I couldn't find anything on the official documentation. If I used the Postman app to Post data everything works however whenever I sent data from my frontend this happens. I wonder if I has anything to do with how i'm sending the data but I don't want to include too much unnecessary.
Thanks in advance for any help :)
print (serializer.errors)spit out?read_only=Trueto directions and **ingredients **