2015-11-20 83 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