How to fix Exception Value: 'NoneType' object has no attribute 'title'

Submitted 3 years, 10 months ago
Ticket #13
Views 340
Language/Framework Django
Priority Low
Status Closed

I am trying to link each comment to a post and to a username

I have proceeded with the logic of the comment with the following models.py

class Comment(models.Model):
    post = models.ForeignKey(
        Post, on_delete=models.CASCADE, null=True, blank=True)
    user = models.ForeignKey(
        User, on_delete=models.CASCADE, null=True, blank=True)
    content = models.TextField(max_length=160)

    def __str__(self):
        return self.content

Now that the comment is working and everything I am trying to link the comment section to the post and the user of the comment in the admin so I used the following models.py

class Comment(models.Model):
    post = models.ForeignKey(
        Post, on_delete=models.CASCADE, null=True, blank=True)
    user = models.ForeignKey(
        User, on_delete=models.CASCADE, null=True, blank=True)
    content = models.TextField(max_length=160)


    def __str__(self):
        return '{}-{}'.format(self.post.title, str(self.user.username)) <----Error line

Which is returning

AttributeError at /admin/score/comment/ 
'NoneType' object has no attribute 'title'
Exception Type: AttributeError
Exception Value:    
'NoneType' object has no attribute 'title'

What should fix this problem I have also used the following which did not work

    def __str__(self):
       return '%s %s' % (self.post.title, self.user.username)
Submitted on May 30, 20
add a comment

1 Answer

Verified

I just started a new db to avoid this error, thanks anyways

Submitted 3 years, 10 months ago

Nice to hear.. its resolved.,

- Vengat 3 years, 10 months ago

Nice to hear.. its resolved.,

- Vengat 3 years, 10 months ago


Latest Blogs