I have 2 different tables in my database. They have some variables common and some different. For example:
Table1:
- ID
- Date
- Name
- Address
- Fax
Table2:
- ID
- Date
- Name
- Telephone number
I want to display data together sorted by date & ID but from both tables. For example, fi开发者_C百科rst displayed will be the newest record from first table, but the second one will be the record from another table posted right after first one.
Hope everybody understand, sorry for my English.
Cheers.
Select entries from both models, than put them into a single list and sort them. Like that:
result = (list(first_query) + list(second_query))
result.sort(cmp=foo)
return result
where foo is a function, which is used to compare two elements:
def foo(a, b):
if a.date > b.date:
return 1
if a.date < b.date:
return -1
if a.date == b.date:
if a.id > b.id:
return 1
if a.id < b.id:
return -1
return 0
And the to display:
<table>
{% for object in result %}
{{ object.render_table }}
{% endfor %}
</table>
and in models (just one table):
class Table1(models.Model):
..
def render_table(self):
return '<tr><td>table1</td></tr>'
精彩评论