Skip to content Skip to sidebar Skip to footer

Django Model Form Many To Many Field Is Not Displaying Value

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

Solution 1:

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) :

classAccountFilters(models.Model):
    name = models.CharField(max_length=255)
    # other attributesdef__unicode__(self):
        return self.name

classAccountParameters(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) :

classAccountFilters(models.Model):
    name = models.CharField(max_length=255)
    # other attributesdef__str__(self):
        return self.name

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

    def__str__(self):
        return self.acctFilterName.name

Post a Comment for "Django Model Form Many To Many Field Is Not Displaying Value"