Home > Mobile >  How to only return one particular column?
How to only return one particular column?

Time:11-09

I have model class Bid in my auction app that stores bids placed on items:

class Bid(models.Model):
    title = models.CharField(max_length=64, blank=True)
    date_time = models.DateTimeField(default=timezone.now, blank=True)
    price = models.DecimalField(max_digits=4, decimal_places=2)
    user = models.CharField(max_length=64)

I want to query all bid prices placed on a particular item:

bids = Bid.objects.all().filter(title=title)

Returns all items with that title, but I just want the price column. I tried :

bids = bids.price

But it didn't work.

CodePudding user response:

If you need only the prices based on the title of bid :

bids = Bid.objects.all().filter(title=title).values_list("price", flat = True)

CodePudding user response:

You need to use a loop to get all prices:

bids = Bid.objects.filter(title=title)
for bid in bids:
    print(bid.price)

This is because the query returns a list.

  • Related