Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I still getting 'DeferredAttribute' object has no attribute 'objects'?

Tags:

python

django

After a few days of searching, I still am unable to get over this hurdle. I'm just trying to print a list of descriptions from Sellers as a view. Here's what I'm working with...

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)

'''

Any direction would be appreciated, I feel like I've read every related post related to this. Figured it was about time to post a question with my exact setup. Thanks!

like image 603
nopaynenogain Avatar asked Dec 18 '25 09:12

nopaynenogain


1 Answers

Sellers.description refers to the field, so you get basically the TextField object, not one of the descriptions of an object, since Sellers is a class, not a Sellers object. You can obtain the description values with:

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)})

Furthermore you can not simply wrap that in a HttpResponse, since that expects a string/bytes-like object. You can for example JSON encode it with a JsonResponse.

like image 79
Willem Van Onsem Avatar answered Dec 20 '25 23:12

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!