Timestamp updating value over every item in a list rather than each one

I have two Django classes representing two different lists:

models.py

class Playinglist(models.Model):
    title = models.CharField(max_length=200)
    user = models.CharField(max_length=64)
    game_id = models.IntegerField()
    started_on = models.DateTimeField(auto_now=True)

class Playedlist(models.Model):
    title = models.CharField(max_length=200)
    user = models.CharField(max_length=64)
    game_id = models.IntegerField()
    finished_on = models.DateTimeField(auto_now=True)

And two functions that add data from one list to another:

def add_to_playinglist(request, game_id):
    if obj:
        obj.delete()
        game = Game.objects.get(id=game_id)
        playing_game = Playinglist.objects.filter(game_id=game_id, user=request.user.username)
        return redirect("profile")
    else:
        obj = Playinglist()
        obj.user = request.user.username
        obj.game_id = game_id
        obj.save()
        game = Game.objects.get(id=game_id)
        playing_game = Playinglist.objects.filter(game_id=game_id, user=request.user.username)
        return redirect("profile")
    
def add_to_playedlist(request, game_id):
    obj = Playedlist.objects.filter(game_id=game_id, user=request.user.username)

    if obj:
        obj.delete()
        game = Playinglist.objects.get(game_id=game_id)
        played_game = Playedlist.objects.filter(game_id=game_id, user=request.user.username)
        return redirect("profile")
    else:
        obj = Playedlist()
        obj.user = request.user.username
        obj.game_id = game_id
        obj.save()
        
        game = Playinglist.objects.get(game_id=game_id)
        game.delete()

        played_game = Playedlist.objects.filter(game_id=game_id, user=request.user.username)
        return redirect("profile")

And to organize/display that data I am using this function:

def profile(request):
    playinglist = Playinglist.objects.filter(user=request.user.username)
    playinglist_items = []
    playing = 0
    present_in_playinglist = False
    if playinglist:
        for item in playinglist:
            try:
                game = Game.objects.get(id=item.game_id)
                playinglist_items.append(game)
                present_in_playinglist = True
                playing += 1
                playinglist = Playinglist.objects.get(id=item.game_id)
                
            except:
                present_in_playinglist = False

    playedlist = Playedlist.objects.filter(user=request.user.username)
    playedlist_items = []
    finished = 0
    present_in_playedlist = False
    if playedlist:
        for item in playedlist:
            try:
                game = Game.objects.get(id=item.game_id)
                playedlist_items.append(game)
                present_in_playedlist = True
                finished += 1
                playedlist = Playedlist.objects.get(game_id=item.game_id)

            except:
                present_in_playedlist = False

    context = {
        "playinglist": playinglist,
        "playedlist": playedlist,
        "present_in_playlist": present_in_playlist,
        "playlist_items": playlist_items,
        "saved": saved,
        "present_in_playinglist": present_in_playinglist,
        "playinglist_items": playinglist_items,
        "playing": playing,
        "present_in_playedlist": present_in_playedlist,
        "playedlist_items": playedlist_items,
        "finished": finished
    }

    return render(request, "capstone/profile.html", context)

Then I am using an html page to display the data:

{% if present_in_playinglist %}
   {% for game in playinglist_items %}
      <p>Started on: {{ playinglist.started_on }}</p>
   {% endfor %}
{% else %}
   <h6>No games currently playing.</h6>
{% endif %}

{% if present_in_playedlist %}
   {% for game in playedlist_items %}
      <p>Started on: {{ playedlist.finished_on}}</p>
   {% endfor %}
{% else %}
   <h6>No games played.</h6>
{% endif %}

Right now When I add an item to a list I am getting the timestamp for when I added it. However, when I add another item to the list it will add the timestamp it has for that newest item to every other item in the list, so I end up with every item in that respected list having the same timestamp. What can I add or rearrange that allows me to have the timestamps accurately reflect when they were originally added to that list? Would be happy to share more code if it may help!

Notice that you’re iterating over playinglist_items where each instance would be identified by game, but you are rendering playinglist.started_on.

Hi, thanks for the reply. In more of the html code that I didn’t post I am taking data from another model so it looks more like this:

{% if present_in_playinglist %}
   {% for game in playinglist_items %}
        <img src="{{ game.image }}">
        <a href="{% url 'game_view' game.title game.id %}"><h5>{{ game.title }}</h5></a>
        <p>Started on: {{ playinglist.started_on }}</p>
{% endif %}
   {% endfor %}

and the model I am taking from looks like this:

class Game(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField(blank=True, null=True, max_length=500)
    image = models.URLField(blank=True, null=True, max_length=500)
    genre = models.CharField(blank=True, null=True, max_length=200)
    platform = models.CharField(blank=True, null=True, max_length=200)
    developer = models.CharField(blank=True, null=True, max_length=200)
    publisher = models.CharField(blank=True, null=True, max_length=200)
    added_by = models.CharField(max_length=64)

    def __str__(self):
        return f"ID# { self.id }: { self.title }"

Would there be a way to iterate over each started_on for each item in the playinglist_items list? Would I need to create another list to store those values? Thanks for the help!

Can you provide a more detailed and specific explanation for this issue? Perhaps tie this to the actual source code / template code involved?

My add_to_playinglist function will add game to my Playinglist model. When I iterate over every item in my playinglist, it will update the timestamp saved for the latest model to every game in my playinglist_items, so every item will have the same timestamp. I’m wondering how I can just get the timestamp for each item and return that in my html page rather than it update them all to the same timestamp.