1

In models.py I have class Memo:

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)

In views.py I am getting warning Unresolved attribute reference 'objects' for class Memo:

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
  • so is the field .objects present for each instance or for the entire class Memo? As the answer below mentions, the confusion most likely comes from differentiating between the class Memo and instances (memo) Commented Mar 17, 2019 at 17:16

1 Answer 1

1

You are using the uppercase Memo in your for loop, referring to the class as a whole instead of the memo instances in all_memos. Try this:

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

As an aside, your model attributes would conventionally be written as month_name, project_name etc. vs MonthName, ProjectName, etc.

Sign up to request clarification or add additional context in comments.

2 Comments

It seems like @Yopa was having an issue with the line containing Memo.objects.all(). Wouldn't they still have the issue with that line?
The issue was certainly with trying to access Memo.MonthName rather than memo.MonthName (same for ProjectName). The original poster was trying to access an instance-level attribute on a reference to the class, rather than on the instance itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.