我是Django的新手。我有一个mySQL查询:
SELECT username, foto FROM a INNER JOIN b ON (a.user_id = b.id) INNER JOIN c ON (a.foto_id = c.id)
如何在Django中编写它?
模型:
class b(models.Model):
username = models.CharField(max_length=30)
email = models.CharField(max_length=30)
password = models.CharField(max_length=30)
class c(models.Model):
user_id = models.IntegerField()
foto = models.FileField(upload_to='documents')
created_at = models.DateTimeField(auto_now_add=True)
class a(models.Model):
user_id = models.IntegerField()
foto = models.ForeignKey('c', blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
答案 0 :(得分:3)
我认为您正在寻找select_related():
A.objects.select_related('b', 'c').values('b__username', 'c__foto')