2

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

4
  • The .author is None, it does not matter if you are logged in or not, you apparently did not save the author to refer to a User object. Commented Dec 20, 2020 at 2:55
  • 1
    message.author can be null, you are not handling this case when accessing message.author.username Commented Dec 20, 2020 at 2:56
  • @WillemVanOnsem i refer the author to User in the class and User=get_user_model() Commented Dec 20, 2020 at 5:07
  • @WillemVanOnsem know of any way I can solve it?.. Commented Dec 20, 2020 at 6:47

2 Answers 2

3

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) } 
Sign up to request clarification or add additional context in comments.

3 Comments

Thx but it Didnt work for me... the same error pops up and the "if not self.author" statement doesnt even get executed....
i made some changes n tried so it works..but i need to add this if condition everywhere else i wanna use the username
Yes, of course. If you want to access an attribute of something, you need to ensure it exists everywhere you want to use it. Making it a property of your model would make this much easier and avoid the repetition of the condition.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.