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!