I tried so many different ways but can't seem to get it.
I have 'TeamName' as a foreign key to 'MLBGame'. I have been trying to create new MLBGame objects but cant get it to work because of the foreign key. What is the correct way to do it?
I have tried querying the foreign key like: team = TeamName.objects.get(name=whatevername) and I tried with (name__name=whatevername)
and then inserting the team var in the new object to create but no go.
I really appreciate any help you can offer, I have been working on this for awhile and the Django docs just have me going around in circles.
Models
class TeamName(models.Model):
name = models.CharField(max_length=100, null=False, blank=False)
abbreviation = models.CharField(max_length=100, null=False, blank=False)
twitter_id = models.CharField(max_length=100, default=name)
def __str__(self):
return self.name
class MLBGame(models.Model):
gameID = models.IntegerField(null=True, blank=True)
name = models.ForeignKey('TeamName',
null=False, blank=False, on_delete=models.CASCADE)
other stuff
team = TeamName.objects.get(name=game.away_team)
MLBGame.objects.update_or_create(
gameID=gameID, defaults={
'name': team.name,
Other Stuff}
)