'QuerySet' object has no attribute
Asked Answered
O

3

6

Please could you me why I get this error message while displaying "fav" in my template

QuerySet object has no attribute game_id

I tried to replace game_id by game, id_game but nothing...

view.py

from django.contrib import messages
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import render
from start.models import Games, FavoriteGames
import urllib, json

def view_recap(request):
    if request.user.is_authenticated():
        username = request.user.username
        id_user = request.user.id
        fav = FavoriteGames.objects.filter(user_id=id_user).game_id
        return render(request, 'recap.html', locals())
    else:
        from start.views import view_logoff
        from start.views import view_logon
        messages.add_message(request, messages.INFO, 'Vous devez être connecté pour accéder à cette page.')
        return redirect(view_logon)

models.py

from django.db import models
from django.conf import settings

# Create your models here.
class Games(models.Model):
    guid = models.CharField(max_length=100, unique=True, null=False, verbose_name="GUID")
    title = models.CharField(max_length=100, null=False, verbose_name="Titre")
    logo = models.CharField(max_length=100, null=True, blank=True, verbose_name="Logo")
    date = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="Date de création")
    update = models.DateTimeField(auto_now=True, verbose_name="Dernière modification")
    def __str__(self):
        return self.title

class FavoriteGames(models.Model):
    game = models.ForeignKey('Games')
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
Octa answered 31/5, 2017 at 12:29 Comment(0)
B
8

When you loop through the queryset you can access the game_id for each instance. You can't access it on the queryset.

You can loop through the queryset in the view,

favs = FavoriteGames.objects.filter(user_id=id_user)

for fav in favs:
    game_id = game_id

or in the template:

{% for fav in favs %}
  {{ fav.game_id }}
{% endfor %}

If you only need the game_ids from the queryset, you could use values_list:

game_ids = FavoriteGames.objects.filter(user_id=id_user).values_list('game_id', flat=True)
Brigand answered 31/5, 2017 at 12:36 Comment(1)
Thank you @BrigandOcta
G
4

filter will return Queryset.So use get instead of filter. If multiple objects are there use filter, but you need to loop over that queryset to get each objects.

Gezira answered 31/5, 2017 at 12:33 Comment(4)
filter return several results. So I need it I think. Get return only 1 result and so I have an error telling there are too many results.Octa
this is the error message with get : get() returned more than one FavoriteGames -- it returned 2!Octa
So multiple game_id will be there.So FavoriteGames.objects.filter(user_id=id_user).values_list(game_id, flat=True) will get all game_idsGezira
I have : SyntaxError: invalid character in identifier. I don't know what does it mean ?Octa
V
0

According to the docs, you can force evaluation of a QuerySet by calling list() on it. For example:

entry_list = list(Entry.objects.all())

This will query the database and return a list of items instead of a QuerySet.

Vinita answered 29/5, 2023 at 21:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.