Gotcha. I’ll try my best to explain what you could try. And if this looks sloppy, then I’ll make a public gist.
So, firstly, let’s set some things straight. A database is a collection of tables. Models in Django represent one table with multiple columns. Tables do not need to have the same columns. What you’ve given is one table and appending lots of new attributes. That’s a non-relational database that I advise you stay away from.
Here’s what I have in mind: there is a profile table. All users in this table are in all contests.
We’re gonna first ignore the Profile model from that link. That’ll be the thing that houses student’s grade. Django has its OWN Users model which the Profile table extends from. Again, let’s ignore the Profile, for now. First import the Django user model:
from django.contrib.auth.models import Users
Then, based on what I’m imagining, we’re going to make one table called ContestID that can include Contest name(optional) and the main behemoth, ContestScore:
import uuid
class ContestID(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=True)
# You don’t have to have the id field since Django automatically enables it if another attribute/column isn’t specified as primary key. I just wanted to show everything
class ContestScores(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
contest = models.ForeignKey(ContestID, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.SET_NULL)
score = models.PositiveIntegerField(default=0)
Alr, here’s what’s goin’ on. We have multiple contests listed in ContestID. If they don’t have names, you can delete the names column/line-of-code.
Then in the table ContestScores, we have an id field called UUIDField. In regular relational databases, row ids are unique. In your spreadsheet, each record/row was unique by an incrementing row number. For us, this might be several gazillionbillionakfhwjbxin rows, so we wanna be safe with this thing called Universally Unique Identifier.
The contest column is a foreign key. This means the that this row belongs to Contest 1 or Contest 2, etc.
The score column is this user’s score based on a certain contest. The user column specifies which user this record of scores belongs to.
So in its entirety, the Contest table would “read” like this: in Contest 1, the user Derek received a score of 94. We can find this record at the id: ajbi02-kebo... (it’s 32 chars long).
Ok, it looks pretty useless right now.
Now, let’s implement that Profile table:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
grade = models.PositiveSmallIntegerField()
index = models.PositiveIntegerField()
Before I move on, I just need to clarify something: what is the index? Is it s/a, and is the index unique for each student. Is “a” the average of all scores across all contests?
The ranking system, if I’m not mistaken, is ranking the players the ENTIRE TIME. So the number one person should have the highest index, s/a.
I’m clear on what the index is utilized for: ranking. I’m just not so clear on how the index is calculated.
If you decide you wanna learn this yourself, the best way is to learn what a relational database is, like MySQL or PostgreSQL or SQLite. Just a small visual explanation or just seeing a database like those will help you in the long run.
But if you decide not to learn too much, because sometimes Django becomes a lot too quickly, just clarify what the index is, and we’ll keep going.