經過幾天的尋找,我仍然無法跨過這個障礙。我只是想打印一份來自賣家的描述列表作為視圖。這就是我正在使用的...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。
添加回答
舉報
0/150
提交
取消