Paging depending on the grouping of elements in Django

For a website implemented in Django / Python, we require the following:

The watch page displays 15 posts per web page. If there are more than two or more messages from one source that follow each other in a view, they should be grouped together.

Perhaps not clear, but with the following example it might be:

Example (with 5 posts per page this time):

  Message1 Source1
  Message2 Source2
  Message3 Source2
  Message4 Source1
  Message5 Source3
  ...

This should be shown as:

Message1 Source1
Message2 Source2 (click here to 1 more message from Source2)
Message4 Source1
Message5 Source3
Message6 Source2

Thus, on each page, a fixed number of elements are displayed on the page where some of them were rearranged.

We are interested in how we can create a Django or MySQL query to query this data in an optimal and simple way. Please note that paging is used and that messages are sorted by time.

PS: , - SQL, .

+5
2

, . -, , .

#In your model
head = models.BooleanField(default=True)

#As a signal plugin:
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()

:

messages = Message.objects.filter(head=True).order_by('time')[0:15]

... , , . /, , ( , , python Lock , , ). , .

, , , , . , , : 30 , , , , 15, , . , , , ?

, , , Lock RLock . :

import thread
lock = thread.allocate_lock()
def check_head(sender, **kwargs):
    # This check must come outside the safe zone
    # Otherwise, your code will screech to a hault
    message = kwargs['instance']
    if hasattr(message,'no_check_head') and message.no_check_head:
        return
    # define safe zone
    lock.acquire()
    # see code above
    ....
    lock.release()

, .

EDIT: (, Apache) , . . . , .

+3

, , . , regroup. :

{% regroup records by source as grouped_records %}
{% for group in grouped_records %}
  {% for item in group.list %}
    <li{% if not forloop.first %} style="display:none"{% endif %}>
       {{ item.message }} {{ iterm.source }}
       {% if forloop.first %}
         {% ifnotequal group.list|length 1 %}
           <a href="#" onclick="...">Show more from the same source...</a>
         {% endifnotequal %}           
       {% endif %}
    </li>
  {% endfor %}
{% endfor %}

, : . 15 , , , - , .

+1

All Articles