DRF PrimaryKeyRelatedField,AttributeError:' QuerySet对象没有属性参数'

时间:2017-07-21 15:39:14

标签: python django django-rest-framework

我试图启动并运行REST后端,这是我在Python中的第一个。 事件可能有许多UnderWays,但UnderWay可能只有一个事件(ManyToOne)。

我现在正试图让UnderWay模型的序列化器工作,它应该使用以下参数执行POST和GET请求['事件',' phonenumber&#39 ;,'时间']。

感觉好像我已经尝试了所有东西,但我得到的只是一个属性错误,说'查询集''对象没有属性'事件'。

models.py:

class Incident(models.Model):
active = models.BooleanField(default=False)
message = models.TextField(max_length=200, blank=True)
time = models.TimeField(auto_now=False, auto_now_add=False)
created_at = models.DateTimeField(auto_now=True, auto_created=True)
updated_at = models.DateTimeField(auto_now=True)


class UnderWay(models.Model):
    id = models.BigAutoField(primary_key=True)
    incident = models.ForeignKey(Incident, null=True)
    telephone = models.CharField(max_length=30, blank=True)
    time = models.CharField(max_length=30, blank=True)
    created_at = models.DateTimeField(auto_now=True, auto_created=True)

views.py

class LastIncidentApiView(generics.ListCreateAPIView):
"""This class defines the create behavior of our rest api."""
queryset = [Incident.objects.order_by('created_at')[0]]
serializer_class = IncidentSerializer

def perform_create(self, serializer):
    """Save the post data when creating a new incident."""
    serializer.save()


class UnderWayApiView(generics.ListCreateAPIView):
    """This class defines the create behavior of our rest api."""
    # active = Incident.objects.latest('created_at').pk
    queryset = [UnderWay.objects.all()]
    serializer_class = UnderWaySerializer

    def perform_create(self, serializer):
        """Save the post data when creating a new incident."""
        serializer.save()

serializers.py

class IncidentSerializer(serializers.ModelSerializer):

class Meta:
    """Meta class to map serializer's fields with the model fields."""
    model = Incident
    fields = ('id', 'active', 'message',
               'time',
              'created_at', 'updated_at')
    read_only_fields = ('created_at', 'updated_at')


class UnderWaySerializer(serializers.ModelSerializer):
    incident = serializers.PrimaryKeyRelatedField(queryset=Incident.objects.all().values_list('pk', flat=True))

    class Meta:
        model = UnderWay
        fields = ('incident', 'telephone', 'time', 'created_at')
        read_only_field = 'created_at'

但是,如果我将序列化程序中的事件字段设置为ReadOnlyField,那么我确实可以使用GET请求。但这对我帮助不大。

感谢任何帮助:)

2 个答案:

答案 0 :(得分:0)

在序列化程序中更改此行,

incident = serializers.PrimaryKeyRelatedField(queryset=Incident.objects.all())

在您看来,

queryset = UnderWay.objects.all()

答案 1 :(得分:0)

要获得更清晰的代码,您应该使用

定义IncidentApiView
queryset=Incident.objects.all()

并且没有perform_create(无用的IMO,正如你所做的那样来自ListCreateAPIView的Create部分)

LastIncidentView可以是一个ListAPIView,你可以覆盖perform_read只获取最后一个(没有理由发布“最后一次事件”,你只想发一个事件,这个事件一次就是当前的事件)

对于UnderWayApiView,queryset应为

UnderWay.objects.all()

对于UnderWaySerializer,

incident = serializers.PrimaryKeyRelatedField(queryset=Incident.objects.all())

应该更好

我并不是说它会解决你的问题,但它应该更加尊重DRF的期望