0

I am creating sample rest API using django-rest-framework, I refereed the tutorial on there website https://www.django-rest-framework.org/tutorial/1-serialization/ I api is working fine when list and create new object but it throwing the exception during detail view(http://127.0.0.1:8000/cars/1) of object.

I have added my code snippet below, Please let me know what wrong i am doing

Models.py

class Car(models.Model):

    created = models.DateTimeField(auto_now_add=True)
    name = models.CharField(max_length=100, blank=True, default='')
    price = models.TextField()

    class Meta:
        ordering = ('created',)

serializers.py

class CarsSerializer(serializers.ModelSerializer):

    class Meta:
        model = Car
        id = serializers.IntegerField(read_only=True)
        fields = ('id', 'created', 'name', 'price')

Views.py

@csrf_exempt
def car_list(request):
"""
List all code cars, or create a new car.
"""
    if request.method == 'GET':
        cars = Car.objects.all()
        serializer = CarsSerializer(cars, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = CarsSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)


@csrf_exempt
def car_detail(request, pk):
"""
Retrieve, update or delete a code cars.
"""
    try:
        car = Car.objects.get(pk=pk)
    except Car.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = CarsSerializer(Car)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = CarsSerializer(Car, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        Car.delete()
        return HttpResponse(status=204)

Urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    path('cars/', views.car_list),
    path('cars/<int:pk>/', views.car_detail),

]

1 Answer 1

2

First of all you need to post exception.

But the problem is that you pass class and not instance of class to your serializer.

try:
    car = Car.objects.get(pk=pk)
except Car.DoesNotExist:
    return HttpResponse(status=404)

if request.method == 'GET':
    serializer = CarsSerializer(car)  #  <-- car not Car (this type of error should be fixed everywhere)
    return JsonResponse(serializer.data)

And in Car.delete() should be car.delete()

Sign up to request clarification or add additional context in comments.

2 Comments

the exception <h1>TypeError at /cars/1/</h1> <pre class="exception_value">int() argument must be a string, a bytes-like object or a number, not &#39;DeferredAttribute&#39;</pre> <table class="meta">
this was the typo error but not sure why its give exception in as TypeError

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.