Can anyone explain why I got this error: 'User' object has no attribute 'notifications'
I have new model Notification and write in file context_processors.py next one:
def notifications(request):
if request.user.is_authenticated:
count = request.user.notifications.filter(is_viewed=False).count()
return {
'notifications_count': count
}
else:
return dict()
def notify(request):
if request.user.is_authenticated:
notifications = Notification.objects.all()
return {
'notifications': notifications
}
else:
return dict()
My model Notification in the same app that is a User and my models looks like this:
class User(AbstractUser):
class Roles(models.IntegerChoices):
......
class Notification(models.Model):
user = models.ForeignKey('User', models.CASCADE)
message = models.TextField('Message', max_length=250, default=None)
is_viewed = models.BooleanField('Seen', default=False)
created_at = models.DateTimeField(auto_now_add=True, null=True)
def __str__(self):
return f"{self.message}"
class Meta:
verbose_name = 'MSG'
verbose_name_plural = 'MSG'
ordering = ['-created_at',]
Please help me to unserstand what is the problem with User object in context_processors.py