2017-02-20 133 views
0

我有一個模型:Django如何獲得外鍵的價值?

from django.contrib.auth.models import User 

# Create your models here. 
class Strategy(models.Model): 
    user = models.ForeignKey(User, on_delete=models.CASCADE) 
    mod_date = models.DateTimeField(auto_now=True) 
    strategy_name = models.CharField(max_length=20) 
    strategy = models.TextField() 
    position = models.TextField() 

我想要得到的用戶名= 「獅子座」。

但是在Strategy的用戶正在使用user_id。

如何使用username =「leo」獲取「models.Strategy.objects.get」或「models.Strategy.objects.filter」?

謝謝。

+0

[由外鍵Django模型濾波器(的可能的複製http://stackoverflow.com/questions/11508744/django -models濾波器逐外鍵) –

回答