Answer the question
In order to leave comments, you need to log in
How to properly combine fields from different tables in Django?
Please tell me how to correctly combine fields from two different tables in the output by a unique identifier. One of these tables is generated by an external program.
Each entry in the first table corresponds to one entry in the second table.
Table I
action_id
name
action
data
action_id
result
Answer the question
In order to leave comments, you need to log in
Here's how I did it.
...
class Action(models.Model):
action = models.CharField('Действие', max_length=13)
name = models.CharField('ФИО', max_length=200)
data = models.DecimalField('Данные', decimal_places=2, max_digits=8)
class Meta:
db_table = 'actions' # задаем свое имя таблицы в базе для этого класса
def __unicode__(self):
return unicode(self.clientid)
class ActionResult(models.Model):
action = models.OneToOneField('Action', to_field='id', related_name='actionresult', primary_key=True,
db_column='action_id')
result = models.CharField('Итог', max_length=45)
class Meta:
db_table = 'results' # задаем свое имя таблицы в базе для этого класса
managed = False
def __unicode__(self):
return unicode(self.src)
urlpatterns = patterns('',
...
url(r'^history/$', views.history, name='history'),
...
)
def history(request):
last_hist = myapp.models.action.objects.all()
context = {
'last_hist': last_hist,
}
return render(request, 'myapp/index.html', context)
{% if last_hist %}
<table width="60%" border="1" cellpadding="4">
<tr align="right" valign="top">
<td>Имя</td>
<td>Данные</td>
<td>Результат</td>
{% for act in last_hist %}
<tr align="right" valign="top">
<td>{{ act.name }}</td>
<td>{{ act.data }}</td>
<td>{{ act.actionresult.result }}</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>No actions are available.</p>
{% endif %}
Start here https://docs.djangoproject.com/en/1.8/howto/legacy...
perhaps the magic will work
In general, it looks like a strange relationship like OneToOneField or ForeignKey
And when you have a model, presumably in the form
#models.py
class Blah1(Model):
# action_id =pk
name = TextField
action = TextField
data = DataTime
blah2 = OneToOneField('Blah2')
class Blah2(Model):
# action_id =pk
result = TextField
# urls.py
# ....
url(r'^show-blah/(?P<pk>\d+)$', view_show_blah, name="view_show_blah"),
# ....
#templates/show-blah.html
<body>
{{ blah }} <br>
{{ blah.name }} {{ blah.data }} {{ blah.blah2.result }}
</body>
#views.py
def view_show_blah(request, pk):
context = {'blah': get_object_or_404(Blah2, pk=pk)}
return render(request, 'show-blah.html', context)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question