如何使用Django显示正确的JSON数据

时间:2019-03-23 19:00:30

标签: django

正在尝试将数据库记录显示为json数组。如果我运行下面的代码

views.py

from django.core import serializers
from django.http import HttpResponse

def read(request):
    members = Member.objects.all()
    print("success")
    jsondata = serializers.serialize('json', members)
    return HttpResponse(jsondata, content_type='application/json')

models.py

from django.db import models
class Member(models.Model):
    firstname = models.CharField(max_length=40)
    lastname = models.CharField(max_length=40)

    def __str__(self):
        return self.firstname + " " + self.lastname

正在按照以下方式获取json响应

[
{"model": "crud.member", "pk": 1, "fields": {"firstname": "Thor", "lastname": "Odinson"}},  
{"model": "crud.member", "pk": 6, "fields": {"firstname": "sdd", "lastname": "rrrrr"}}
]

我的要求: 这就是我想要的。我想按以下方式获取json响应

[
{"firstname": "Thorr", "lastname": "Odinson"},  
{"firstname": "Ann", "lastname": "bell"}
]

为此,如果我尝试按照以下代码使用JsonResponse()方法,则会出现错误 “ QuerySet”对象不可调用

def read(request):
    #response_data = {}
    response_data = []
    members = Member.objects.all()
    return JsonResponse(response_data, members)
    print("success")

1 个答案:

答案 0 :(得分:0)

对于您要寻找的输出,在> corpus <<VCorpus>> Metadata: corpus specific: 0, document level (indexed): 0 Content: documents: 2000 的输出上调用//add a constructor to your component constructor(props) { super(props); //add this context to the move function to access this.bikes this.move = this.move.bind(this); } move(e) { let bikeId = e.target.value; let bikes = this.bikes; // ... do whatever you have to do with bikeId and bikes } render() { //... <Button.Success value={bike.BikeID} onClick={this.move}> Move to Status 1 </Button.Success> //... } 应该没问题:

#include < iostream >
using namespace std;
int main()
{
    int n,i,s=0,p=1;
    cin>>n;
    for (i=1;i<=n;i++)
         s=s+i;
         p=p*i;

        cout<<s<<" "<<p;
}