V
V
Vladimir2015-04-29 22:33:53
Django
Vladimir, 2015-04-29 22:33:53

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

Table II
action_id
result

As a result, I need to display a table on the page made up of the fields: name, data, result
I do not ask for a ready-made code, I just ask to describe how it is done correctly in Django.
I just started to get acquainted with the framework, so far I have little idea what's what.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2015-04-30
@koef

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 %}

S
sim3x, 2015-04-29
@sim3x

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)

# code not tested

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question