Вы можете использовать собственный менеджер, который упрощает фильтрацию пользователей в нескольких представлениях.
class PromoNotificationManager(models.Manager):
def get_for_user(self, user)
"""Retrieve the notifications that are visible to the specified user"""
# untested, but should be close to what you need
notifications = super(PromoNotificationManager, self).get_query_set()
user_filter = Q(groups__in=user.groups.all())
group_filter = Q(users__in=user.groups.all())
return notifications.filter(user_filter | group_filter)
Подключите менеджер к вашей модели PromoNotification:
class PromoNotification(models.Model):
...
objects = PromoNotificationManager()
Тогда, на ваш взгляд:
def some_view(self):
user_notifications = PromoNotification.objects.get_for_user(request.user)
Подробнее о пользовательских менеджерах вы можете прочитать в документации: http://www.djangoproject.com/documentation/models/custom_managers/