未解析的属性引用'对象'

时间:2017-11-05 15:29:52

标签: python django

在models.py中,我有类备忘录:

class Memo(models.Model):
    MonthName = models.ForeignKey(Months, on_delete=models.CASCADE)
    ProjectName = models.ForeignKey(Project, on_delete=models.CASCADE)
    Hours = models.IntegerField(blank=True, null=True)
    # ProjectManager = models.ForeignKey(ItEmployee, on_delete=models.CASCADE)

    def __str__(self):
        return str(self.MonthName) + ' - ' + str(self.ProjectName)

在views.py中,我收到警告未解析的属性引用'对象'课堂备忘录:

from django.http import HttpResponse
from History.models import Memo

def memos(request):
    all_memos = Memo.objects.all()
    html = ''
    for memo in all_memos:
        url = '/memo/' + str(Memo.MonthName) + '/'
        html += '<a href="' + url + '">' + Memo.ProjectName + '</a><br>'
    return HttpResponse(html)

1 个答案:

答案 0 :(得分:1)

您在for循环中使用大写Memo,指的是整个类,而不是memo中的all_memos个实例。试试这个:

for memo in all_memos:
    url = '/memo/' + str(memo.MonthName) + '/'
    html += '<a href="' + url + '">' + memo.ProjectName + '</a><br>'

顺便说一下,您的模型属性通常会写为month_nameproject_name等对MonthNameProjectName等。