为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我仍然得到“DeferredAttribute”对象没有属性“对象”?

为什么我仍然得到“DeferredAttribute”对象没有属性“对象”?

aluckdog 2023-06-13 14:58:03
经过几天的寻找,我仍然无法跨过这个障碍。我只是想打印一份来自卖家的描述列表作为视图。这就是我正在使用的...models.py:from django.db import modelsclass Sellers(models.Model):    index = models.BigIntegerField(blank=True, null=False)    seller = models.TextField(db_column='SELLER', blank=False, null=False,                          primary_key=True)    block = models.TextField(db_column='BLOCK', blank=False, null=False)      street = models.TextField(db_column='STREET', blank=False, null=False)      space = models.TextField(db_column='SPACE', blank=False, null=False)     description = models.TextField(db_column='DESCRIPTION', blank=True, null=True)    document_with_idx = models.TextField(blank=False, null=False)     document_with_weights = models.TextField(blank=False, null=False)class Meta:    managed = False    db_table = 'Sellers'def __str__(self):    return self.index'''views.py:from django.http import HttpResponsefrom search.models import Sellersdef search(request):    output = Sellers.description.objects.all()    return HttpResponse(output)'''任何方向将不胜感激,我觉得我已经阅读了与此相关的所有相关帖子。我想是时候用我的确切设置发布问题了。谢谢!
查看完整描述

1 回答

?
MYYA

TA贡献1868条经验 获得超4个赞

Sellers.description指的是字段,所以你基本上得到的是对象,而不是对象中的TextField一个,因为它是一个类,而不是一个对象。您可以通过以下方式获取值:descriptionsSellersSellersdescription


from django.http import JsonResponse

from search.models import Sellers


def search(request):

    output = Sellers.objects.values_list('description', flat=True)

    return JsonResponse({'data': list(output)})

此外,您不能简单地将其包装在 a 中HttpResponse,因为它需要一个类似字符串/字节的对象。例如,您可以使用 JSON 对其进行编码JsonResponse。


查看完整回答
反对 回复 2023-06-13
  • 1 回答
  • 0 关注
  • 94 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信