2015-11-20 80 views
0

我在模型表單上有多對多字段和一個外鍵字段。它似乎在做正確的查詢,但結果是對象而不是對象的值。Django模型表單多對多字段不顯示值

是否需要使用VALUES_LIST方法覆蓋查詢集?

forms.py

class Meta: 
    model = AccountParameters 
    fields =['acctFilterName', 'excludeClassification', 'tradingCash',] 
    #exclude = ['acctFilterName'] 
    labels = { 
     'acctFilterName': _('Account Filters:'), 
     'excludeClassification': _('Exclude Classifications: '), 
     'tradingCash':_('Remove accounts whose trading cash < % of AUM: ') 
    } 

models.py

class AccountParameters(models.Model): 
acctFilterName = models.ForeignKey(AccountFilters) 
excludeClassification = models.ManyToManyField(ClassificationNames) 
tradingCash = models.FloatField() 

Screenshot of multiselect

回答

0

你必須添加一個方法用一個字符串來表示你的對象:

如果您使用python 2,請使用__unicode__,並在python 3上使用:__str__

E.g(與Python 2):

class AccountFilters(models.Model): 
    name = models.CharField(max_length=255) 
    # other attributes 

    def __unicode__(self): 
     return self.name 

class AccountParameters(models.Model): 
    acctFilterName = models.ForeignKey(AccountFilters) 
    excludeClassification = models.ManyToManyField(ClassificationNames) 
    tradingCash = models.FloatField() 

    def __unicode__(self): 
     return self.acctFilterName.name 

E.g(與Python 3):

class AccountFilters(models.Model): 
    name = models.CharField(max_length=255) 
    # other attributes 

    def __str__(self): 
     return self.name 

class AccountParameters(models.Model): 
    acctFilterName = models.ForeignKey(AccountFilters) 
    excludeClassification = models.ManyToManyField(ClassificationNames) 
    tradingCash = models.FloatField() 

    def __str__(self): 
     return self.acctFilterName.name