我想在JSON響應中的country_name字段中獲取所有值。如何獲取django模型字段中的值?
這裏是我的models.py:
from django.db import models
class Countries(models.Model):
country_name = models.CharField(max_length=100)
def __str__(self):
return str(self.country_name)
這裏是爲了得到它:
from django.http import Http404
from django.shortcuts import HttpResponse
from .models import Countries
import json
from django.core import serializers
def AllCountries(request):
countries = list(Countries.objects.all())
data = serializers.serialize('json', countries)
return HttpResponse(data, mimetype="application/json")
這裏是JSON響應我得到:
[{「PK 「:1587,」model「:」interApp.countries「,」fields「:{」country_name「:」bangladesh「}}]
但我不想要」pk「和「模型」,我只想要所有的國家名稱。
https://docs.djangoproject.com/en/1.9/topics/serialization/#subset-of-fields –