1

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}
                     )
2
  • Can you paste the error here ? Commented Aug 31, 2021 at 20:11
  • django.core.exceptions.FieldError: Unsupported lookup 'name' for CharField or join on the field not permitted. Commented Aug 31, 2021 at 20:17

1 Answer 1

1

Figured out my issue. I was trying to get the name out of the TeamName object. in MLBGame.objects.update_or_create I hade 'name': team.name. Should have just been team.

Correct code:


team = TeamName.objects.get(name=somename)

MLBGame.objects.update_or_create(
                    gameID=gameID, defaults={
                        'name': team}
                     )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.