I'm sorry Dev, but that is really bad practice.

You are doing in python what can be done by the database. That type of code
would create a memory error and be very slow to run. It's much better to
get the last item from the database. In your example it would just be:
last_action = ActionGame.objects.last()


Den tis 28 feb. 2023 kl 15:04 skrev Dev Femi Badmus <[email protected]
>:

> all_action=ActionGame.objects.all()
> my_action = []
> for action in all_action:
>     my_action.append(action)
> last_action = my_action[-1]
>
>
> On Tue, Feb 28, 2023 at 1:05 PM Andréas Kühne <[email protected]>
> wrote:
>
>> Se comments below.
>>
>>
>> Den fre 24 feb. 2023 kl 12:14 skrev Byansi Samuel <
>> [email protected]>:
>>
>>> Hey everyone, l got a problem. Am trying to retrieve a single latest
>>> object added everytime, and display it in the template.
>>> Below is my codes but;
>>>
>>> Help me figure it out the source of the problem in these different
>>> instances below 👇
>>>
>>> _______________________ issue number 1_______
>>>
>>> #Action/models.py
>>> class ActionGame(models.Model):
>>>         name=models.Charfield()
>>>         published=models.DateTimeField()
>>>
>>> #Action/views.py
>>> from Action.models import ActionGame
>>>
>>> def action (request):
>>>         latest_action=ActionGame.objects.filter(published=published).
>>> latest()
>>> Context={ 'latest_action': latest_action }
>>> return....
>>>
>>> But it returns *error* :
>>> name 'published' is not defined
>>>
>>
>> You haven't defined what the value of published is. If you want to get
>> the last created one the query should be:
>> latest_action=ActionGame.objects.latest("published") - that would give
>> you the last published item. Another way would be:
>> latest_action=ActionGame.objects.order_by("published").last()
>>
>>
>>> ____________________ issue number 2________
>>>
>>> #Action/models.py
>>> class ActionGame(models.Model):
>>>         name=models.Charfield()
>>>         published=models.DateTimeField()
>>>         class Meta:
>>>               get_latest_by='published'
>>>
>>> #Action/views.py
>>> ........
>>> latest_action=ActionGame.objects. latest()
>>> .......
>>>
>>> #but it returns *error* :
>>> 'ActionGame' object is not iterable
>>>
>>> Even if I try this:
>>> ............
>>> latest_action=ActionGame.objects. latest('published')
>>> .......
>>>
>>> #it returns the Same error:
>>> 'ActionGame' object is not iterable
>>>
>>> But this second issue, the error is referred in #action.html
>>>
>>> {% for x in latest _action %}
>>> <p>{{ x.game_name }}</p>
>>> {% endfor %}
>>>
>>>
>> So the latest_action variable that you have sent to the template is an
>> instance of ActionGame - and not a list or anything. You can only access
>> variables in the ActionGame class. For example:
>>
>>  <p>{{ lastest_action.name }}</p>
>>
>> would print out the value of the name of the ActionGame instance.
>>
>> Please l need your assistance.
>>>
>>> I'm Samuel,
>>> Thanks
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CAGQoQ3wMUno6hLAO-1FtN0Nn7VtkCn4qf-O4U%3DpeJ4JzY%2B%3DcAQ%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/django-users/CAGQoQ3wMUno6hLAO-1FtN0Nn7VtkCn4qf-O4U%3DpeJ4JzY%2B%3DcAQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAK4qSCeBCST7o-5WZx%2BmhGO2yH-Msdse%3DKusk9UOyW8XZBXRBw%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CAK4qSCeBCST7o-5WZx%2BmhGO2yH-Msdse%3DKusk9UOyW8XZBXRBw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAD9bWYwtxjinYmeNWZvBdCdQB5zAa3je1bwMjUhmzEjzF0Q_bg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAD9bWYwtxjinYmeNWZvBdCdQB5zAa3je1bwMjUhmzEjzF0Q_bg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK4qSCcNoEZ18G5NTmOGqnPRfrEAGChinvPdLVJYDsF1%2BEQyuA%40mail.gmail.com.

Reply via email to