0
我正在查詢我的數據庫並檢索字段的所有實數值。 而是我正在獲取外鍵id值。 任何人都可以幫忙嗎?查詢Django中的外鍵數據
這裏是我的models.py:
class Respondant(models.Model):
Respond_Id = models.IntegerField(null=True)
Gender = models.TextField(null=True)
Age = models.TextField(null=True)
Country = models.ForeignKey(Country, related_name="respondant_Country")
Theme = models.ForeignKey(Question, related_name="respondant_Theme")
Question = models.ForeignKey(Question, related_name="respondant_Question")
def __str__(self):
return " Respondant:{} ".format(self.Respond_Id)
的RespondantSerializer:
class RespondantSerializer (serializers.ModelSerializer):
class Meta:
model = Respondant
fields=('Respond_Id' ,'Gender' , 'Age', 'Answer')
我的查詢中api.py設置:
class Theme1Api(ListAPIView):
queryset = Respondant.objects.filter(Theme=1)
serializer_class = RespondantSerializer
name = 'theme1'
def get_queryset(self, *args, **kwargs):
Country = self.kwargs['Country']
Question = self.kwargs['Question']
return Respondant.objects.filter(Theme=1).filter(Question=Question).filter(Country=Country)
這裏是一個輸出:
[{"Respond_Id": 258, "Gender": "Female", "Age": "18-21", "Answer": 424},
{"Respond_Id": 258, "Gender": "Female", "Age": "18-21", "Answer": 428}]
我想你在你的json響應中使用了國家,主題和問題? 'RespondantSerializer'的外觀是什麼? – slider
謝謝你的回答。不,我想要回答真正的價值而不是id。我在上面添加了RespondantSerializer。 – road