Osborne Saka
Osborne Saka

Reputation: 509

How to set views in django restframework

I am experimenting with django rest framework. I want to build a simple CRUD to post products to the database. However, I get an error when i visit the url to post the product.

serializers.py

from rest_framework import serializers
from .models import Product


class ProductSerializer(serializers.ModelSerializer):
    model = Product
    fields = ("id", "name", "brand", "specs", "price", "stock", "picture")

views.py

from rest_framework import viewsets
from .serializers import ProductSerializer
from .models import Product


class ProductViewSet(viewsets.ModelViewSet):
    serializer_class = ProductSerializer
    queryset = Product.objects.all()

This is the error i get when I got to the url to post product 'tuple' object has no attribute 'values'

Upvotes: 1

Views: 31

Answers (1)

kayoz
kayoz

Reputation: 1144

Both model and fields in your serializer should be in an inner Meta class.

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = ["id", "name", "brand", "specs", "price", "stock", "picture"]

Upvotes: 1

Related Questions