Closed kellylarrea closed 2 years ago
Could you include what you've console.logged and if it was successful?
raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'pets'
I saw that above. Line by line it might be helpful to console.log /print** the different attempts you've made to get pets. You may need to use request.POST.get()
We solved this issue just now but now running into another with our partial update request
def partial_update(self, request, pk):
"""Update Request"""
# Locate Review
# get_object_or_404 returns a object representation of our Mango
review = get_object_or_404(Review, pk=pk)
# Check the review's owner against the user making this request
if request.user != review.pet_owner:
raise PermissionDenied('Unauthorized, you do not own this review')
# Ensure the owner field is set to the current user's ID
review_user = request.user
review_data = Review(pet_owner = review_user)
# Validate updates with serializer
data = ReviewSerializer(review_data, data=request.data, partial=True)
if data.is_valid():
# Save & send a 204 no content
data.save()
return Response(status=status.HTTP_204_NO_CONTENT)
# If the data is not valid, return a response with the errors
return Response(data.errors, status=status.HTTP_400_BAD_REQUEST)
the error I'm getting is
'ReviewsDetail' should either include a queryset
attribute, or override the get_queryset()
method.
Fixed our initial problem to post Pets with this code
def post(self, request):
pet_user = request.user
pet_data = Pet(pet_owner = pet_user)
pet = PetSerializer(pet_data, data=request.data)
if pet.is_valid():
# Save the created mango & send a response
pet.save()
return Response(pet.data, status=status.HTTP_201_CREATED)
# If the data is not valid, return a response with the errors
return Response(pet.errors, status=status.HTTP_400_BAD_REQUEST)
[https://www.django-rest-framework.org/api-guide/serializers/] this might help.
[https://www.django-rest-framework.org/api-guide/routers/] and this
What stack are you using?
(ex: MERN(mongoose + react), DR(django + react), PEN, etc.)
We are using DR
What's the problem you're trying to solve?
We are trying to create a post route for our pets and reviews but it's not working.
Post any code you think might be relevant (one fenced block per file)
If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?
django.utils.datastructures.MultiValueDictKeyError: 'pets'
What is your best guess as to the source of the problem?
I believe we are creating the request incorrectly. Not grabbing the pet object correctly
What things have you already tried to solve the problem?
We have tried changing the code multiple times referencing other code alongs but no solution yet.