2014-09-27 58 views
1

我正在編寫一個Django應用程序,它從單獨的Bugzilla數據庫中獲取錯誤信息。我的設置有兩個數據庫條目,非默認爲bugzilla。當使用db_manager時,'QuerySet'對象沒有屬性'login_name'

我創建了一個模型,其中包含我想要從Bugzilla配置文件表(包含用戶)所需的字段。

class Bugzilla_profiles(models.Model): 
    class Meta: 
     db_table = 'profiles' 

    userid = models.PositiveIntegerField(primary_key=True) 
    login_name = models.CharField(max_length=255) 
    realname = models.CharField(max_length=200) 

    def __unicode__(self): 
     return self.login_name 

當我嘗試在Django shell中讀取數據並嘗試讀取其中一個字段時,它說沒有屬性。我知道它應該在那裏,因爲如果我只是打印變量,它通過unicode函數顯示正確的屬性。我嘗試訪問所有3個字段,並且他們都給出了相同的錯誤。

>>> from bugscrub.models import Bugzilla_profiles 
>>> bz_assigned_to = Bugzilla_profiles.objects.db_manager('bugzilla').filter(userid=11) 
>>> bz_assigned_to 
[<Bugzilla_profiles: [email protected]>] 
>>> bz_assigned_to.login_name 
Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
AttributeError: 'QuerySet' object has no attribute 'login_name' 
>>> 

回答

3

filter返回QuerySet對象,其中不包含列名作爲屬性。您可以使用get代替:

bz_assigned_to = Bugzilla_profiles.objects.db_manager('bugzilla').get(userid=11) # changed filter to get 

或者你也可以在第零位置的查詢集的檢索對象(例如):

bz_assigned_to[0].login_name # the login_name attribute of the first 'row' in the QuerySet