I have the following SQL
statement that I know works:
SELECT year_table.year_value, races.race_name, results.category,results.position
FROM races, year_table, results
WHERE year_table.year_value = variable AND year_table.year_id = results.year_id;
How would you convert this to Django
query statements?
These are the models:
class YearTable(models.Model):
year_id = models.IntegerField(primary_key=True)
year_value = models.CharField(max_length=10, blank=True)
class Meta:
managed = False
db_table = 'year_table'
class Races(models.Model):
race_id = models.IntegerField(primary_key=True)
race_name = models.CharField(max_length=50, blank=True)
class Meta:
managed = False
db_table = 'races'
class Results(models.Model):
year = models.ForeignKey('YearTable')
race = models.ForeignKey(Races)
category = models.CharField(max_length=100)
position = models.CharField(max_length=10)
class Meta:
managed = False
db_table = 'results'
您可以从结果中获取所有信息,并使用select_related
加入其他模型。
results = Results.objects.all().select_related('year', 'race')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.