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)
I just started a new db to avoid this error, thanks anyways