Django模型表单很多字段不显示值 [英] Django Model Form Many to Many field is not displaying value
本文介绍了Django模型表单很多字段不显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用VALUES_LIST方法覆盖查询集吗? p>
forms.py
class Meta:
model = AccountParameters
fields = ['acctFilterName','excludeClassification','tradingCash',]
#exclude = ['acctFilterName']
labels = {
'acctFilterName':_('Account Filters:'),
'excludeClassification':_('Exclude Classifications:'),
'tradingCash':_('删除帐户的交易现金<% AUM:')
}
models.py p>
class AccountParameters(models.Model):
acctFilterName = models.ForeignKey(AccountFilters)
excludeClassification = models。 ManyToManyField(ClassificationNames)
tradingCash = models.FloatField()
解决方案
您必须添加一个方法来使用字符串来表示您的对象:
如果您使用的是python 2,请使用 __ unicode __
和python 3使用: __ str __
。
例如python 2):
class AccountFilters(models.Model):
name = models .CharField(max_length = 255)
#其他属性
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 sel f.acctFilterName.name
例如(使用python 3):
class AccountFilters(models.Model):
name = models.CharField(max_length = 255)
#other属性
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
I have a many to many field and a foreign key field on a model form. It appears to be making the right query but the result are Objects and not the values of the object.
Do I need to overwrite the queryset using a VALUES_LIST method?
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()
解决方案
You have to add a method to represent your object with a string :
If you are using python 2, use __unicode__
and on python 3 use : __str__
.
E.g (with 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 (with 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
这篇关于Django模型表单很多字段不显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文