我遇到了浏览我的rest_framework api页面时无法解决的错误。
完整错误(Django错误)是:
Got AttributeError when attempting to get a value for field `process` on
serializer `ResultSerializer`.
The serializer field might be named incorrectly and not match any attribute
or key on the `Shop` instance.
Original exception text was: 'Shop' object has no attribute 'process'.
似乎序列化程序试图在另一个名为ResultSerializer的序列化程序中给出的字段中获取值,但却找不到它。我检查了所有字段都是正确的。
这是我的models.py
from django.db import models
class ResultSet(models.Model):
process = models.CharField(max_length=10)
subprocess = models.CharField(max_length=10)
class Shop(models.Model):
Establishment = models.CharField(max_length=100)
Address = models.CharField(max_length=100)
Suburb = models.CharField(max_length=50)
Postcode = models.CharField(max_length=10)
State = models.CharField(max_length=5)
Establishment_Type = models.CharField(max_length=20)
latitude = models.DecimalField(decimal_places=6, max_digits=12)
longtitude = models.DecimalField(decimal_places=6, max_digits=12)
class Meta:
ordering = ('Establishment',)
class EntirelyResult(models.Model):
Result = models.ManyToManyField(Shop, related_name='fullresult')
Status = models.ManyToManyField(ResultSet, related_name='status')
这是我的serializers.py
from rest_framework.serializers import ModelSerializer
from .models import Shop, ResultSet, EntirelyResult
class ResultSerializer(ModelSerializer):
class Meta:
model = ResultSet
fields = ('process', 'subprocess')
class ShopSerializer(ModelSerializer):
class Meta:
model = Shop
fields = ('__all__')
class ShopDetailSerializer(ModelSerializer):
Result = ResultSerializer(many=True, read_only=True)
Status = ShopSerializer(many=True, read_only=True)
class Meta:
model = EntirelyResult
fields = ('Result', 'Status')
这是我的views.py
from rest_framework.generics import ListAPIView
from .models import EntirelyResult
from .serializers import ShopDetailSerializer
class ShopDetailAPIView(ListAPIView):
queryset = EntirelyResult.objects.all()
serializer_class = ShopDetailSerializer
我有什么理由让rest_framework正确吗?
答案 0 :(得分:4)
序列化程序ShopDetailSerializer
中有一个拼写错误:
class ShopDetailSerializer(ModelSerializer):
Result = ShopSerializer(many=True, read_only=True)
Status = ResultSerializer(many=True, read_only=True)
编辑:
CompleterelyResult将Result
字段映射到Shop
和Status
字段到ResultSet
,而序列化程序最初将Result
字段映射到ResultSet
和Status
字段到Shop