1👍
I think the general idea of your model approach is good enough. Nevertheless, I’d encourage you to go through Django’s ORM relationships documentation. It’s ok if you want to use strings as your primary key, but let Django’s ORM know that by using the proper model field. If you use primary and foreign keys (relationships), your dbms will create indexes over the necessary fields, resulting in more efficient queries. Let me know if this help! Good luck with your project!
UPDATE:
It would be something like this:
class Day(models.Model):
today = models.DateField(default=date.today)
class Game(models.Model):
game_outcome = models.CharField(max_length=8)
game_quarter = models.CharField(max_length=1)
game_clock = models.PositiveSmallIntegerField()
day = models.ForeignKey(Day, on_delete=models.CASCADE)
away_team = models.ForeignKey(Team, on_delete=models.CASCADE, related_name='away_team')
away_team_score = models.PositiveSmallIntegerField()
home_team = models.ForeignKey(Team, on_delete=models.CASCADE, related_name='home_team')
home_team_score = models.PositiveSmallIntegerField()
class Team(models.Model):
team_class = models.CharField(max_length=3)
team_city = models.CharField(max_length=13)
team_name = models.CharField(max_length=12)
Notice that I omitted all id
fields, since django will use one automatically created, which behaves the same as AutoField
. Also notice that since Game
has two references to the same class (Team
), it is necessary to provide the extra parameter related_name
. Mor info about it in django docs about related_name
.
This way, you can do things like
game = Game.objects.find(1)
away_team_name = game.away_team.team_name
You can access the associated Team
class in the away_team
variable. No need to call away_team_id
at all.
1👍
First of all you need to understand the more ForeignKeys you use the more load you put on the database. But it also depends a lot on the views.py code as well. Second of all you don’t need to use team_id
as a field name if you use a ForeignKey. Just mention the class name and it will automatically create an _id
for the field.
Look at the following code:
class Game(models.Model):
start_time = models.DateTimeField()
today = models.DateField(default=date.today)
game_id = models.AutoField(primary_key=True) # You don't need this line, an id field is created for EVERY model class even if you don't specify it.
game_outcome = models.CharField(max_length=8)
game_quarter = models.CharField(max_length=1)
game_clock = models.PositiveSmallIntegerField()
away_team = models.ForeignKey('Team')
home_team = models.ForeignKey('Team')
class Team(models.Model):
team_class = models.CharField(max_length=3)
city = models.CharField(max_length=13)
name = models.CharField(max_length=12)
score = models.PositiveSmallIntegerField()
You can see that I have created another class by the name of Team
and put four fields on that which was repeated in your original Game
class. Then I have connected the Team
class to the Game
class with ForeignKey
. If you install SQLiteBrowser and check out the database file then you will see that the away_team
field is actually the away_team_id
field in the database because Django takes care of that. You don’t need to create id
fields, just use relations like ForeignKey
, ManyToManyField
, etc. Off the top of my head this is the only thing that I can suggest.
Judging by the scope of this project you will not put too much pressure on the database with too many relations, so don’t worry about that now. That’ it. If you have any question more specific you can ask. Cheers! 🙂
P.S. I am not sure about your project details so use whichever relation you see fit, be it one-to-one, many-to-one or many-to-many, etc. Read the docs for more details. 🙂
- [Answered ]-TemplateDoesNotExist in Heroku using Django Rest Framework
- [Answered ]-Select in (select ..) using ORM django
- [Answered ]-How to import packages in virtualenv in python shell