Reputation: 53
I am trying to create a chat app and got stuck with this error. I am getting this error though I'm logged in as a superuser.
My models.py
class Message(models.Model):
author = models.ForeignKey(User, null=True, related_name='author_messages', on_delete=models.CASCADE)
content = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.author.username
def last_10_messages():
return Message.objects.order_by('-timestamp').all()[:10]
Where I try to access it:
for message in messages:
print("message printing")
result.append(self.message_to_json(message))
then
def message_to_json(self, message):
print("message.id=",message.id)
print(message.author.username)
return {
'id': message.id,
'author': message.author.username,
'content': message.content,
'timestamp': str(message.timestamp)
}
When i print the length of the object i notice it says 2..idk why coz i haven't added any messages yet. As the loop goes twice i noticed that the username got printed the first time but raised an error for the second loop(though idk why it loops coz i dont even have messages to load yet) like here
The error also appears to be in the return function in my models class as in here
I've read other posts but their errors were different...
Would be really grateful if sum1 cud help out!!
or how do i define and access author variables the correct way
Upvotes: 2
Views: 2437
Reputation: 3717
It seems that you have more the one message even if you said you did not create any. At least you must have created one with the username aysha. The message with the id=1
seems to have no user attached ... so you get the error when trying to access message.author.username
.
To add the if not self.author
in the __str__
of Message
does not change anything as you access or print message.author.username
and not message. It would help if you print(message)
and message has no author.
Upvotes: 0
Reputation: 12849
If you allow the author
to not be set then you need to make sure it has been set before you try to use it.
Be a little defensive like this;
class Message(models.Model):
author = models.ForeignKey(User, null=True, related_name='author_messages', on_delete=models.CASCADE)
content = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
if not self.author:
return "Anonymous"
return self.author.username
def last_10_messages():
return Message.objects.order_by('-timestamp').all()[:10]
def message_to_json(self, message):
if message.author:
author = message.author.username
else:
author = "Anonymous"
return {
'id': message.id,
'author': author,
'content': message.content,
'timestamp': str(message.timestamp)
}
Upvotes: 3