如何获取Django模板以从两个不同的模型中提取信息? [英] How to get a django template to pull information from two different models?

查看:70
本文介绍了如何获取Django模板以从两个不同的模型中提取信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编码一个基本的django应用程序,它将基于MariaDB数据库中的信息显示当前商店的销售表.

I am coding a basic django application that will show a table of current store sales, based off of information from a MariaDB database.

数据是通过单独的过程输入数据库的,因此不是在Django中创建的,它只是使用简单的python脚本来加载csv文件并将其解析为Insert查询.我的代码中有两个模型,Stores和ShowroomData. Showroomdata保留python脚本中的所有记录,但是不保留任何商店信息.我希望它能够显示所有陈列室数据以及未存储在ShowroomData模型中的商店的位置标题.我知道我需要分离模型,但无法弄清楚如何使它们链接在一起.

The data is entered into the database through a seperate process, so it isn't created in Django, it is just using a simple python script to load a csv file and parse it into an Insert query. There are two models in my code, Stores and ShowroomData. Showroomdata holds all of the records from the python script, however it does not hold any store information. I would like for it to be able to show all of the showroom data as well as the store's location title which is not stored in the ShowroomData model. I know I need to seperate models but can not figure out how to get them to link together.



class ShowroomData(models.Model):
    storeid = models.IntegerField(default=0)  # Field name made lowercase.
    date = models.DateField()  # Field name made lowercase.
    time = models.TimeField()  # Field name made lowercase.
    sales = models.DecimalField(max_digits=10, decimal_places=2)  # Field name made lowercase.
    tax = models.DecimalField(max_digits=10, decimal_places=2)  # Field name made lowercase.
    class Meta:
        unique_together = (('storeid', 'date', 'time'),)
        db_table = 'showroomdata'


class Stores(models.Model):
    storeid = models.IntegerField(primary_key=True)
    location = models.CharField()

我希望它能够像这样输出一个表:

I would like for it to be able to output a table like so:

StoreID-位置-日期-时间-销售-税收

StoreID - Location - Date - Time - Sales - Tax

这是我的WIP html文件.

Here is my WIP html file.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Trickle</title>
  </head>
  <body>
    <h1>Current Showroom Data</h1>

    {% if current_showroom %}

        <table>
            <thead>
              <th>Store Number</th>
              <th>Location</th>
              <th>Date</th>
              <th>Sales</th>
              <th>Tax</th>
            </thead>

            {% for store in current_showroom %}
              <tr>
                <td>{{ store.storeid }}</td>
              </tr>
            {% endfor %}
        </table>




    {% endif %}
  </body>
</html>

推荐答案

实际上是外键的ShowroomData模型上的storeid字段.所以您应该这样声明:

The storeid field on the ShowroomData model of actually a foreign key. So you should declare it as such:

class ShowroomData(models.Model):
    store = models.ForeignKey("Stores", db_column="storeid")

现在,您可以在模板中遵循该fk.假设current_showroom是ShowroomData实例的查询集:

Now you can follow that fk in your template. Assuming current_showroom is a queryset of ShowroomData instances:

        {% for store in current_showroom %}
          <tr>
            <td>{{ store.storeid }}</td>
            <td>{{ store.store.name }}</td>
          </tr>
        {% endfor %}

这篇关于如何获取Django模板以从两个不同的模型中提取信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆