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
.authorisNone, it does not matter if you are logged in or not, you apparently did not save theauthorto refer to aUserobject.