在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)
答案 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_name
,project_name
等对MonthName
,ProjectName
等。