Carrie Hunt
Carrie Hunt

Reputation: 11

django query two tables

I am very new to django and have working through a bunch of the tutorials but they all seem to stop short of actually having the django site code on how to do more complex queries from the database.

I have the following models:

class game(models.Model):
abbreviation = models.CharField(max_length=15)
name = models.CharField(max_length=50)

class environment(models.Model):
name = models.CharField(max_length=20)
gameName = models.ForeignKey(game, related_name='environment', unique=True)
gameID = models.IntegerField()

I need to be able to do a query that has the gameID from the environment table but needs to look it up on the game table and return the name. There is only one game per environment.

Thanks, Carrie

Upvotes: 1

Views: 177

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

environment.objects.filter(gameName__name=somename).get()

source

Upvotes: 1

Related Questions