def check_head(sender, **kwargs):
    message = kwargs['instance']
if hasattr(message, 'no_check_head') and message.no_check_head:
    return
previous_message = Message.objects.filter(time__lt=message.time).order_by(
    '-time')[0]
if message.source == previous_message.source:
    message.head = False
next_message = Message.objects.filter(time__gt=message.time).order_by('time')[0
    ]
if message.source == next_message.source:
    next_message.head = False
next_message.no_check_head
next_message.save()
Paging depending on grouping of items in Django