What is the simpliest way to figure out does notification belong to BaseNotification or to ExtendedNotification?
class User(models.Model):
pass
class BaseNotification(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='notifications')
class ExtendedNotification(BaseNotification):
pass
# usage
for notification in user.notifications.all():
# --> here <--
>Solution :
You can distinguish the two by using hasattr(notification, 'extendednotification'). Here’s an example of how to use it with a loop:
for notification in user.notifications.all():
if hasattr(notification, 'extendednotification'):
extended_notification = notification.extendednotification
# do stuff with the extended notification
else:
# do stuff with the base notification