我正在尝试使用django rest框架。我想构建一个简单的CRUD,将产品发布到数据库中。但是,当我访问URL发布产品时收到错误消息。

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


这是我到达产品发布网址时遇到的错误
“元组”对象没有属性“值”

最佳答案

序列化程序中的modelfields都应在内部的Meta类中。

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

关于python - 如何在Django Rest框架中设置 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58325862/

10-16 04:23