如果不包含 Django 插入到列表中

Django insert into list if doesn't contain it

我想在通知列表中插入每种类型的一条通知。我有这个:

初始查询的结果(我猜是带有查询集的列表),名为 notifications:

[<Notification: Should be first>, <Notification: New link>]

我的限制是:

   for(note in notification):
        if len(note.region.all()) == 0 and (note.notificationType.priority not in notifications):
        notifications.append(note)

我的问题是,如何进入通知列表以获取属性 notificationType.priority 以查看该号码不在 notifications 列表中。

如果我得到你的问题,你可以试试这个:

notificationsPiorities = set([note.notificationType.priority for note in notifications ])
   for(note in notification):
        if len(note.region.all()) == 0 and (note.notificationType.priority not in notificationsPiorities ):
        notifications.append(note)
        notificationsPiorities.add(note.notificationType.priority)

此外,您可能需要重命名变量。我无法确定 notificationnotifications 是什么。一个是您将显示的列表,一个是您通过查询检索的列表?