Django: How do I sort on date from two models? -
1.) have following models.py definition:
django.db import models datetime import date class author(models.model): author = models.charfield(max_length=20) def __unicode__(self): return '%s' % (self.author) class systema(models.model): author = models.foreignkey(author) date = models.datefield() system = models.charfield(max_length=20, blank=false, default="system a") description = models.charfield(max_length=300) status = models.charfield(max_length=6) def __unicode__(self): return '%s, %s, %s, %s, %s' % (self.date, self.author, self.system, self.description, self.status) class systemb(models.model): author = models.foreignkey(author) date = models.datefield() system = models.charfield(max_length=20, blank=false, default="system b") description = models.charfield(max_length=300) status = models.charfield(max_length=6) def __unicode__(self): return '%s, %s, %s, %s, %s' % (self.date, self.author, self.system, self.description, self.status)
2.) admin.py definition:
acc.models import systema, systemb, author django.contrib import admin admin.site.register(systema) admin.site.register(systemb)
3.) , views.py definition:
django.http import httpresponse acc.models import systema, systemb django.template import context, loader itertools import chain operator import attrgetter def index(request): a_list = systema.objects.all().order_by('-date') b_list = systemb.objects.all().order_by('-date') result_list = sorted( chain(a_list, b_list), key=attrgetter('date')) t = loader.get_template('index.html') #c = context({'result_list': result_list,}) c = context({'a_list': a_list,'b_list': b_list,}) return httpresponse(t.render(c))
4.) finally, template presenting static html page (index.html) defined as:
{% if a_list %} <ul> {% in a_list %} <li>{{a.date}} | {{a.author}} | {{a.system}} | {{a.description}} | {{a.status}}</li> {% endfor %} </ul> {% endif %} {% if b_list %} <ul> {% b in b_list %} <li>{{b.date}} | {{b.author}} | {{b.system}} | {{b.description}} | {{b.status}}</li> {% endfor %} </ul> {% endif %}
the code working , i've entered data admin interface. result:
april 18, 2013 | owta | system | jobs went bananas! | failed april 17, 2013 | rash | system | | ok april 18, 2013 | owta | system b | jobs went bananas! | failed april 17, 2013 | rash | system b | | ok
my aim result sorted on date, independently models (systema/systemb) in below sort order:
april 17, 2013 | rash | system | | ok april 17, 2013 | rash | system b | | ok april 18, 2013 | owta | system | jobs went bananas! | failed april 18, 2013 | owta | system b | jobs went bananas! | failed
i've spent haft week trying sort out without success, , i'm noob django. appreciated! thanks.
"i one-model solution before, somehow felt had have different classes/models systems in order fetch data nicely static page sorted on date each system"
well rash, how 1 model. remember, try reduce duplication in system.
models.py
from django.db import models class author(models.model): author = models.charfield(max_length=20) def __unicode__(self): return '%s' % (self.author) class system(models.model): system_choices = (('a','system a'),('b','system b')) author = models.foreignkey(author) date = models.datefield() system = models.charfield(max_length=1, choices=system_choices) # see [1] description = models.charfield(max_length=300) status = models.charfield(max_length=6)
admin.py
from django.contrib import admin class systemadmin(admin.modeladmin): list_filter = ['system'] # see [2] admin.site.register(system, systemadmin)
urls.py
url(r'^/system/(?p<system>a|b)/list/$', views.systemlistview.as_view(), name='system-list' )
views.py
from django.views.generic.list import listview .models import system class systemlistview(listview): model = system context_object_name = "systems" def get_queryset(self): return super(systemlistview, self) \ .filter(system = self.kwargs['system']) \ .order_by(self.request.get.get('sort') or '-date')
{template_folder}/yourapp/system/list.html
<h1> system {{ system }}{# see [3] #} </h1> <ul> {% sys in systems %} <li>{{sys.date}} | {{sys.author}} | {{sys.description}} | {{sys.status}}</li> {% endfor %} </ul>
appendix
[1] https://docs.djangoproject.com/en/stable/ref/models/fields/#choices [2] https://docs.djangoproject.com/en/stable/ref/contrib/admin/#django.contrib.admin.modeladmin.list_filter [3] https://github.com/django/django/blob/master/django/views/generic/detail.py#l99
Comments
Post a Comment