Note that there are some explanatory texts on larger screens.

plurals
  1. POis there a way to use django generic views and some smart urlpatterns for quick ordering/sorting of queries?
    primarykey
    data
    text
    <p>let's assume I have a django model like this:</p> <pre><code>class Event(CommonSettings) : author = models.ForeignKey(User) timestamp = models.DateTimeField(auto_now_add=True) event_type = models.ForeignKey(Event_Type, verbose_name="Event type") text_field = models.TextField() flag_box = models.BooleanField() time = models.TimeField() date = models.DateField() project = models.ForeignKey(Project) </code></pre> <p>now, by default, I have a view where I sort all events by time &amp; date:</p> <pre><code>event_list = Event.objects.filter().order_by('-date', '-time') </code></pre> <p>however, maybe the user wants to sort the events by time only, or by the date, or maybe in ascending order instead of descending. I know that I can create urlpatterns that match all these cases and then pass on the these options to my view, however I feel like I'm reinventing the wheel here. the django admin site can do all of this out of the box.</p> <p>So here's my question: is there a clever, easy way of getting this done in a generic way, or do I have to hard code this for my models / views / templates?</p> <p>and yes, I did find solutions like this (https://gist.github.com/386835) but this means you use three different projects to achieve one thing - this seems to be a too complicated solution for such a simple thing.</p> <p><strong>EDIT1:</strong> how do I have to change the template so that I can combine multiple filters? Right now I have </p> <pre><code>&lt;a href="?sort=desc"&gt;Desc&lt;/a&gt; &lt;a href="?sort=asc"&gt;Asc&lt;/a&gt; </code></pre> <p>but I want to allow the user to also change number of entries that get displayed. So I have:</p> <pre><code>&lt;a href="?order_by=date"&gt;order by date&lt;/a&gt; &lt;a href="?order_by=name"&gt;order by name&lt;/a&gt; </code></pre> <p>This works all fine, but if I click on 'order by date' and then I click on 'Asc', then my previously selected order disappears. That's not what I want. I want the user to be able to combine some options but not others.</p> <p><strong>EDIT2:</strong> unfortunately your solution doesn't work with </p> <pre><code>from django.views.generic.list_detail import object_list </code></pre> <p>and it's</p> <pre><code>paginate_by </code></pre> <p>option.</p> <p>I tried:</p> <pre><code>&lt;a href="?sort={{sort_dir}}?page={{ page_obj.previous_page_number }}" class="prev"&gt;prev&lt;/a&gt; &lt;a href="?sort={{sort_dir}}?page={{ page_obj.next_page_number }}" class="next"&gt;{% trans "next" %}&lt;/a&gt; </code></pre> <p>but the links then just don't work (nothing happens). maybe you need to do something special with "object_list"?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload