Reputation: 81
I am new in Django. I have an mySQL query:
SELECT username, foto FROM a INNER JOIN b ON (a.user_id = b.id) INNER JOIN c ON (a.foto_id = c.id)
How to write it in Django?
models:
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)
Upvotes: 1
Views: 325
Reputation: 111355
I think you are looking for select_related():
A.objects.select_related('b', 'c').values('b__username', 'c__foto')
Upvotes: 3