当我使用json ex创建发帖请求时。
{
"title":"test",
"company" : "test",
"location" :"test",
"link" :"http://www.google.com/1"
}
我收到的答复是:
{"id":538,"link":"http://www.google.com/1"}
为什么我的所有字段都没有保存到数据库?
我已将fields ='__all__'更改为fields =('title','company','location','link'),但出现错误:
/ api / listings /类型为TextField的对象中的TypeError不是JSON 可序列化
from django.db import models
# Model:
class Listing(models.Model):
title = models.TextField(max_length=100,blank=True),
company = models.TextField(max_length=50, blank=True),
location = models.TextField(max_length=50, blank=True),
link = models.URLField(max_length=250, unique=True)
#------------------------------------------------
from rest_framework import serializers
from listings.models import Listing
#Listing Serializer:
class ListingSerializer(serializers.ModelSerializer):
class Meta:
model = Listing
fields = '__all__'
#------------------------------------------------
from listings.models import Listing
from rest_framework import viewsets, permissions
from .serializers import ListingSerializer
#Listing Viewset:
class ListingViewSet(viewsets.ModelViewSet):
queryset = Listing.objects.all()
#.objects.all().delete()
permissions_classes = [
permissions.AllowAny
]
serializer_class = ListingSerializer
答案 0 :(得分:0)
通过使用相同的应用程序设置创建一个新项目,最终解决了我的问题。我假设应用程序模型的初始迁移存在一些问题。