Django模型表单多对多字段不显示值

时间:2022-09-03 19:18:50

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?

我是否需要使用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()

Django模型表单多对多字段不显示值

1 个解决方案

#1


0  

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__ .

如果您使用的是python 2,请使用__unicode__并在python 3上使用:__str__。

E.g (with python 2) :

例如(使用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) :

例如(使用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

#1


0  

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__ .

如果您使用的是python 2,请使用__unicode__并在python 3上使用:__str__。

E.g (with python 2) :

例如(使用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) :

例如(使用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