Jason Howard
Jason Howard

Reputation: 1586

def __str__ on model

I'm looking to return a str on this model that is equal to industry name + risk level (i.e. Low)

RISKLEVEL = (
    (0, "Low"),
    (1, "Medium"),
    (2, "High"),
    )

class IndustryIndex(models.Model):
    industry = models.CharField(max_length=200)
    risk = models.IntegerField(choices=RISKLEVEL)
    description = models.CharField(max_length=200)

    def __str__(self):
        return self.industry + self.page

I know that my syntax on the line under def __str__(self): isn't correct. Do you know what it should be? Also, self.page only seems to return the integer, but I'm interested in returning the description (i.e. 'High').

Thanks!

Upvotes: 0

Views: 104

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47354

You can use get_risk_display() method:

def __str__(self):
    return '{}-{}'.format(self.industry, self.get_risk_display())

Upvotes: 1

Related Questions