0

这只发生在我制作一个模型以从同一模型中继承 2 次时:

class Word(models.Model):
    cuvint = models.ForeignKey(KeyWordsMapping, on_delete=models.CASCADE, related_name="%(class)s_cuvint")
    frequency = models.IntegerField()
    is_stem = models.BooleanField()
    score_nr = models.ForeignKey(KeyWordsMapping, on_delete=models.CASCADE, related_name="%(class)s_score_nr")
    department_id = models.ForeignKey(Department, on_delete=models.CASCADE, blank=True, null=True)
    seniority_id = models.ForeignKey(Seniority, on_delete=models.CASCADE, blank=True, null=True)

一切正常,但访问第二页给了我一个空数组:

第一页:

{
    "count": 311,
    "next": "http://127.0.0.1:8000/api/words?limit=1&offset=1",
    "previous": null,
    "results": [
        {
            "cuvint": "QA",
            "frequency": 4,
            "is_stem": false,
            "score_nr": 100,
            "department_id": "Quality Assurance",
            "seniority_id": null
        }
    ]
}

第二页:

{
    "count": 311,
    "next": "http://127.0.0.1:8000/api/words?limit=1&offset=2",
    "previous": "http://127.0.0.1:8000/api/words?limit=1",
    "results": []
}

我还查看了管理面板,同样的问题仍然存在。

风景:

class WordList(mixins.ListModelMixin,
                mixins.RetrieveModelMixin,
                generics.GenericAPIView):
    queryset = Word.objects.all()
    serializer_class = WordSerializer

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

序列化器:

class WordSerializer(serializers.ModelSerializer):
    seniority_id = serializers.SerializerMethodField()
    department_id = serializers.SerializerMethodField()
    score_nr = serializers.SerializerMethodField()
    cuvint = serializers.SerializerMethodField()

    class Meta:
        model = Word
        fields = ("cuvint", "frequency", "is_stem", "score_nr", "department_id", "seniority_id")

    def get_seniority_id(self, obj):
        if obj.seniority_id:
            return obj.seniority_id.name
        return None

    def get_department_id(self, obj):
        if obj.department_id:
            return obj.department_id.name
        return None

    def get_score_nr(self, obj):
        return obj.score_nr.score_nr

    def get_cuvint(self, obj):
        return obj.cuvint.cuvint
4

0 回答 0