Ich möchte alle Werte in meinem Feld Country_name in einer JSON-Antwort erhalten.Wie erhalten Sie die Werte im Django-Modellfeld?
Hier ist meine 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)
und hier ist der Ansicht, es zu bekommen:
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")
Hier ist die JSON-Antwort, die ich erhalten:
[{ "pk ": 1587," model ":" interApp.countries "," fields ": {" country_name ":" bangladesh "}}]
Aber ich will nicht das" pk "und das "Model", ich will nur alle Ländernamen.
https://docs.djangoproject.com/de/1.9/topics/serialization/#subset-of-fields –