I'm not sure the title is the most accurate way of stating my issue, and I'm very new to Django. I'm displaying speedrun entries. It's set up so users can enter their record information in a form, with a dropdown menu for the game, which is a foreign key to a GameName object. They enter their game on a separate page if it wasn't listed. I wanted people to be able to add their games because there are too many for me to list out in the short amount of time I have to do this.
I have a page that displays all games entered into the dB so far. When a user clicks on the game name I would like to redirect to a page that lists all entries entered for that game title. This is where I am having trouble. I need to create a views function that can populate a html table with all relevant values from my object Record but based on the name of the game found in the GameName dB table. I can't come up with the right function to output all records/rows from the Record object that match a specific game_name/GameName pk.
models.py
class GameName(models.Model):
game_name = models.CharField(max_length=60, default="")
objects = models.Manager()
def __str__(self):
return self.game_name
PLATFORM_CHOICES = {...}
class Record(models.Model):
player = models.CharField(max_length=60, default="")
game = models.ForeignKey(GameName, related_name='records', on_delete=models.CASCADE)
time = models.CharField(max_length=30, default="HH:MM:SS")
platform = models.CharField(max_length=60, choices=PLATFORM_CHOICES, default="")
date = models.DateField(default=date.today)
objects = models.Manager()
def __str__(self):
return self.player
My views.py function giving me an issue:
def game_record(request, pk):
gamename = get_object_or_404(GameName, pk=pk)
records = Record.objects.filter('game__game_name')
content = {'gamename': gamename, 'records': records}
return render(request, 'speed_run_game_records.html', content)
Which returned an error of too many values to unpack. This is just the latest of many tries to get this right.
Also tried:
records = Record.objects.filter('game')
Which returned an error of Record not being iterable.
Also tried:
records = Record.objects.select_related('game')
Which also returned an error of Record not being iterable.
I also used the related_name='records' substituted for 'game'.
In case it helps, here is my template:
{% extends 'speed_run_base.html' %}
{% load static %}
{% block title %}{{ gamename.game_name }} Records{% endblock %}
{% block content %}
<h1 class="homeHeading">{{ gamename.game_name }}</h1>
<div class="table-container">
<table class="table">
<thead>
<tr class="tHead">
<th>Player</th>
<th>Time</th>
<th>Platform</th>
<th>Date</th>
</tr>
</thead>
{% for record in records %}
<tbody>
<tr class="tBody">
<td>{{ records.name }}</td>
<td>{{ records.time }}</td>
<td>{{ records.platform }}</td>
<td>{{ records.date }}</td>
</tr>
</tbody>
{% endfor %}
</table>
</div>
{% endblock %}
Any help would be greatly appreciated.
CodePudding user response:
Hmm, instead of:
gamename = get_object_or_404(GameName, pk=pk)
records = Record.objects.filter('game__game_name')
have you tried the following?
gamename = get_object_or_404(GameName, pk=pk)
records = Record.objects.filter(game=gamename)