猿问

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

经过几天的寻找,我仍然无法跨过这个障碍。我只是想打印一份来自卖家的描述列表作为视图。这就是我正在使用的...


models.py:


from django.db import models



class 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 HttpResponse

from search.models import Sellers



def search(request):

    output = Sellers.description.objects.all()

    return HttpResponse(output)

'''


任何方向将不胜感激,我觉得我已经阅读了与此相关的所有相关帖子。我想是时候用我的确切设置发布问题了。谢谢!


aluckdog
浏览 134回答 1
1回答

MYYA

Sellers.description指的是字段,所以你基本上得到的是对象,而不是对象中的TextField一个,因为它是一个类,而不是一个对象。您可以通过以下方式获取值:descriptionsSellersSellersdescriptionfrom django.http import JsonResponsefrom search.models import Sellersdef search(request):    output = Sellers.objects.values_list('description', flat=True)    return JsonResponse({'data': list(output)})此外,您不能简单地将其包装在 a 中HttpResponse,因为它需要一个类似字符串/字节的对象。例如,您可以使用 JSON 对其进行编码JsonResponse。
随时随地看视频慕课网APP

相关分类

Python
我要回答