0

We are loading on the web browser the content of the model "Post", which is ordered by "helium" (our variable for popularity).

Now, we'd like to order the same content by "pop_date" (our variable for due_date) when the user clicks on a button.

Is it possible to do it instantly without reloading the browser page?

The options we've seen until now:

  • change the "order_by" and request a new list ( new operation on the database, reuploading all the medias again)
  • use python "sorted" function (memory issues for many posts loaded)

models.py

class Post(models.Model):
    pub_date = models.DateTimeField('date published',auto_now=True)
    pop_date = models.DateTimeField('Popping time', blank=False)        
    helium=models.IntegerField(default=0)
    (...)

views.py (query)

Post.objects.filter(pop_date__gte=timezone.now()).order_by(self.ordering,'-id')

url.py

# Order by Helium       
url(r'^(?P<slug>[-\w]+)/popular$', views.IndexView.as_view(ordering='-helium'), name='index'),    

# Order by Popping time
url(r'^(?P<slug>[-\w]+)/timeline$', views.IndexView.as_view(ordering='pop_date'), name='index_now'),    
Tim
  • 3
  • 2
  • "reuploading all the medias again": No, web browsers are smart enough and would just reload the HTML content. – mimo Apr 12 '15 at 21:09

1 Answers1

0

I would definitely go with order_by. You can't use sorted after a user clicks on the button, you have to make another request again (you can eventually use JS but that's a whole new story - you will have to fetch all the data you need, this will limit you not to be able to use pagination in the future).

Todor
  • 15,307
  • 5
  • 55
  • 62
  • Thanks for the tip Todor, we will go forward with your advice. By curiosity, how would you use JS to solve this issue ? – Tim Apr 13 '15 at 07:12
  • Using `JS`'s [`Array.sort`](http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects). The flow would be something like - [on button click](http://stackoverflow.com/questions/1947263/using-an-html-button-to-call-a-javascript-function), get all the `post` from the html, and reorder them. Check [this](http://stackoverflow.com/questions/7623819/looking-for-a-javascript-solution-to-reorder-divs) question, there is an example solution. – Todor Apr 13 '15 at 07:31