Ze carlos
Ze carlos

Reputation: 19

Django insert into list if doesn't contain it

I want to insert into the notifications list, one notification of each type. and I have this:

Result from the initial query (I guess is a list with queryset), named notifications:

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

And the restriction that I have is:

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

My question is, how do I get inside the notifications list to get the attribute notificationType.priority to see is that number isn't inside notifications list.

Upvotes: 0

Views: 63

Answers (1)

Sylvain Biehler
Sylvain Biehler

Reputation: 1433

If I get your question, you can try this :

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)

Also, you may need to rename your variables. I can't tell for sure what are notification and notifications. One is the list you will display, and one is the list you retrieve with your query ?

Upvotes: 1

Related Questions