Search code examples
pythondjangotypeerrorpython-docx

Django Choices Reference in View


Model.py:

class RiskIssue(models.Model):
RISK_ISSUE_SEVERITY = (
    ('L', 'Low'),
    ('M', 'Medium'),
    ('H', 'High'),
)
projectRiskIssueSeverity = models.CharField("Risk/Issue Severity", max_length=1, choices=RISK_ISSUE_SEVERITY, default='L')

View.py code:

cell = table.rows[2].cells[0]
formatted_status = [astatus.get_risk_issue_severity_display() for astatus in activitylist.values_list('activityStatus', flat=True)]
cell.paragraphs[0].text = ', '.join(formatted_status)

I am getting the error code: 'unicode' object has no attribute 'get_risk_issue_severity_display'

What am I doing wrong? I have tried everything except the correct answer. Very small TypeError problem here I assume


Solution

  • You can't use get_FOO_display() with a string value as you are trying to do. You would need an instance of the model for it to work. As you have it, what your code is translating to is something like:

    'L'.get_projectRiskIssueSeverity_display()
    

    That's where the "no attribute" error comes from.

    If you just want a list of the possible values you would be better off using RiskIssue.RISK_ISSUE_SEVERITY directly from the view and transforming it to a list or whatever you need.