1

Я пытаюсь вывести на экран имя User. Вот как это выглядит

Вот мой views.py:

bid = Bid.objects.filter(listing_offer_id=itemID).order_by("bid_offer").values()
last_bid = bid.last()# the last value of sorted list of bids

bid_offer = last_bid["bid_offer"] # gets the max value of bid offers   
bid_owner = User.objects.filter(id = last_bid['bid_owner_id'])# gets the owner of the bid

Вот модель Bid:

class Bid(models.Model):
    bid_offer = models.IntegerField()
    listing_offer = models.ForeignKey(Listing, on_delete = models.CASCADE, related_name = "listings", null = True)
    bid_owner= models.ForeignKey(User, on_delete = models.CASCADE)

вот модель Listing:

class Listing(models.Model):
    title = models.CharField(max_length= 64)
    description = models.CharField(max_length= 128)
    img = models.ImageField(upload_to = 'auctions/media/images')
    isActive = models.BooleanField(default= True)
    owner = models.ForeignKey(User, on_delete = models.CASCADE, related_name="user")
    categories = models.ForeignKey(Category, on_delete = models.CASCADE, blank= True, null = True, related_name = "category", default = "None")
    price = models.IntegerField(default = 0)

модель User это AbstractUser

Как вывести просто ismail без скобок и т.д?

1 ответ 1

0

Чтобы вывести имя пользователя bid_owner без скобок и т.д., вы можете использовать атрибут username объекта User.

bid_owner = User.objects.filter(id=last_bid['bid_owner_id'])
print(bid_owner.username)

Но лучше вместо атрибута id использовать pk, т.к. id - это функция Python, а pk - это Джанговская функция.

2
  • там ошибка: 'QuerySet' object has no attribute 'username'
    – 808thlife
    18 фев в 8:34
  • а как у вас называется поле где имя User в модели, то и используете вместо username
    – Leonid
    18 фев в 9:11

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.