django inner join query

后端 未结 1 982
耶瑟儿~
耶瑟儿~ 2021-01-15 07:06

I am working with django and having a hard time grasping how to do complex queries

Here is my model

class TankJournal(models.Model):
    user = model         


        
相关标签:
1条回答
  • 2021-01-15 07:47

    If you already have your tank object you should be able to do:

    tank.user.username
    

    To reduce the database queries you might want to consider the use of select_related(), e.g.

    tanks = TankJournal.objects.all().select_related()
    for tank in tanks:
        username = tank.user.username
    

    if you have a specific tank id then:

    tank = TankJournal.objects.select_related().get(id=123456)
    username = tank.user.username
    
    0 讨论(0)
提交回复
热议问题