gpt4 book ai didi

python - Django 两个内连接

转载 作者:行者123 更新时间:2023-11-30 22:59:38 25 4
gpt4 key购买 nike

我是 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)

最佳答案

我认为您正在寻找 select_related() :

A.objects.select_related('b', 'c').values('b__username', 'c__foto')

关于python - Django 两个内连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35662406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com