Json Records通过Django从数据库仅返回记录的一行 [英] Json Records returns only one rows of records from database via django

查看:30
本文介绍了Json Records通过Django从数据库仅返回记录的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有3条记录,但是json响应仅返回一条记录.

我需要通过实例化变量来通过json显示数据库记录,以代替使用数据库的库仑名称名字姓氏,从数据库输出json记录时,我可以分别使用 fname lname 之类的东西.

为此,我在下面创建了以下代码

views.py

  def读取(请求):response_data = {}对于Member.objects.all()中的成员:#members = Member.objects.all()response_data ['fname'] = members.lastnameresponse_data ['lname'] = members.firstname打印(成功....")jsondata = json.dumps([response_data])#jsondata = json.dumps(list([response_data]))返回HttpResponse(jsondata,content_type ='application/json') 

我的问题:我的问题是,每次运行上述代码时,它仅通过json仅显示数据库中的一条记录.例如.在这里

  [{"fname":"Thorr","lname":"Odinson"}] 

我的要求:

如何使其循环更多并通过json显示数据库中的所有三个记录例如

  [{"fname":"Thorr","lname":"Odinson"},{"fname":"Ann","lname":"bell"},{"fname":"Jon","lname":"Han"}] 

解决方案

您的错误是您在循环内返回了响应.而是先创建响应数据并发送.

  def读取(请求):response_data = [在Members.objects.all()中,m的[{'fname':m.lastname,'lname':m.firstname}]jsondata = json.dumps(response_data)返回HttpResponse(jsondata,content_type ='application/json') 

此外,您的逻辑似乎在翻转名字和姓氏.我保留了此逻辑以使其与您的代码保持一致,但您可能想要反转fname和lname args.

I have three records in database but json response returns only just one records.

I need to display database records via json by instantiating a variables so that instead of using database coulmn name firstname and lastname, I can use something like fname and lname respectively when outputing json records from database.

To this effect, I have created the following code below

views.py

def read(request):
    response_data = {}
    for members in Member.objects.all():
    #members = Member.objects.all()

        response_data['fname']=members.lastname
        response_data['lname']=members.firstname
        print("successful....")
        jsondata = json.dumps([response_data])
        #jsondata = json.dumps(list([response_data]))
        return HttpResponse(jsondata, content_type='application/json')

My Issue: My problem is that each time the code above is run, it displays only just one records from database via json. Eg. Here

[
{"fname": "Thorr", "lname": "Odinson"}
]

My Requirements:

How do I make it to loop more and display all the three records from database via json Eg.

 [
    {"fname": "Thorr", "lname": "Odinson"},  
    {"fname": "Ann", "lname": "bell"},
    {"fname": "Jon", "lname": "Han"}
    ]

解决方案

Your error is that you are returning a response within your loop. Instead create the response data first and the send it.

def read(request):
    response_data = [{'fname': m.lastname, 'lname': m.firstname} for m in Members.objects.all()]
    jsondata = json.dumps(response_data)
    return HttpResponse(jsondata, content_type='application/json')

Also, your logic seems to be flipping first and last names. I kept this logic to align with your code but you may want to reverse the fname and lname args.

这篇关于Json Records通过Django从数据库仅返回记录的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆