WDI-SEA / project-4-issues

Open an issue to receive help on project 4 issues
0 stars 0 forks source link

issue on creating post on django #2

Closed kellylarrea closed 2 years ago

kellylarrea commented 2 years ago

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)

def post(self, request):
        """Create request"""
        # Add user to reqpta object 

        pet_data = request.data['pets']
        pet_user = request.user.id

        pet_data['owner'] = pet_user

        pet = PetSerializer(data=request.data)
        if pet.is_valid():
            # Save the created pet & 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)

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

raise MultiValueDictKeyError(key)

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.

DoireannJane commented 2 years ago

Could you include what you've console.logged and if it was successful?

kellylarrea commented 2 years ago
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'pets'
DoireannJane commented 2 years ago

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()

kellylarrea commented 2 years ago

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.

kellylarrea commented 2 years ago

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)
DoireannJane commented 2 years ago

[https://www.django-rest-framework.org/api-guide/serializers/] this might help.

[https://www.django-rest-framework.org/api-guide/routers/] and this