django - How to organize queries depending on previous queries? -


for example have listview renders objects, have buttons use ajax render these objects different filters, example filter alphabetic order or other field. when use these buttons return filtered result of listview objects. if listview returns objects in published order buttons do:

in published order -> name -> views

how make sequence this:

in publish order publish -> name -> views(by name also) -> else(by views also)

i think possible hass vars ajax request , depending on var return query. don't know if it's best way not django. best way doing this?

example

app/models.py

from django.db import models  class entry(models.model):     title = models.charfield(max_length=100)     content = models.textfield()     pub_date = models.datetimefield(auto_now_add=true)     views = models.integerfield()      def __str__(self):         return self.title 

app/urls.py (if want try example, don't forget include app.urls in project.url)

from django.conf.urls import url . import views  urlpatterns = [     url(r'^$', views.index, name='index'), ] 

app/views.py

from django.shortcuts import render .models import entry  def index(request):     # fields in want order query     ordering_fields = ['pub_date', 'views', 'title']      # order_query field names dots between them     order_query = request.get.get('o')      # ordering ['views', 'tile']     ordering = []     if order_query:         ordering = [x x in order_query.split('.') if x in ordering_fields]     entries = entry.objects.order_by(*ordering)      # @ index.html understand, query_strings     query_strings = {}     field in ordering_fields:         if field in ordering:             # remove field             idx = ordering.index(field)             query_strings[field] = '.'.join(ordering[:idx]+ordering[idx+1:])         else:             # add fieild             query_strings[field] = '.'.join(ordering + [field])      return render(request, 'app/index.html', {         'entries': entries,         'query_strings': query_strings,         'ordering': ordering     }) 

app/templates/app/index.html

<!doctype html> <head>   <meta charset="utf-8">   <title>entries</title>   <style>   table {     border: 1px solid #333;   }   td, th {     border: 1px solid #333;     padding: 5px 10px   }   </style> </head> <body>   <p><a href="{% url 'index' %}">index</a></p>   <p>     sort by:     <a href="?o={{ query_strings.title }}">title</a>{% if 'title' in ordering %}*{% endif %} |     <a href="?o={{ query_strings.views }}">views</a>{% if 'views' in ordering %}*{% endif %} |     <a href="?o={{ query_strings.pub_date }}">pub date</a>{% if 'pub_date' in ordering %}*{% endif %}   </p>   <table>     <thead>       <tr>         <th>title</th>         <th>views</th>         <th>pub date</th>       </tr>     </thead>     <tbody>     {% entry in entries %}       <tr>         <td>{{ entry.title }}</td>         <td>{{ entry.views }}</td>         <td>{{ entry.pub_date }}</td>       </tr>     {% endfor %}     </tbody>   </table> </body> 

urls ordering these: ?o=title, ?o=pub_date.views or ?o=views.title.pub_date.

in last case result ordered views, title, pub_date. link title ?o=views.pub_date, views ?o=views.title , pub date o?=views.title.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -