【问题标题】:Fill-in 2D table with backreferencing使用反向引用填充 2D 表
【发布时间】:2011-06-23 23:07:31
【问题描述】:

我正在尝试使用 Django 构建一个表:

  1. 在行和列之间使用多对多关系。
  2. 允许空格。
  3. 可展开。
  4. 存储可以通过行名和列名检索的值。我正在尝试使用中间模型来执行此操作(不确定这是否是最好的方法)。

我在引用这些值时遇到了困难。我的 HTML 表需要列出所有可能的行和列,我认为我不能通过循环表值来做到这一点,因为有些可能不存在。

这是我正在尝试做的一个示例......

models.py
from django.db import models

class Row(model.Models):
    name = models.CharField(max_length=8)

class Col(model.Models):
    rows = ManyToManyField(Row, through='Table')
    name = models.CharField(max_length=8)

class Table(model.Models):
    row = ForeignKey(Row)
    col = ForeignKey(Col)
    value = models.FloatField()

观点:

views.py
from django.shortcuts import *
from test.app.models import *

def table(request):
    rows = Row.objects.all()
    cols = Col.objects.all()
    values = Table.objects.all()
    return render_to_response(
        'app/table.html',
        {'row_list': rows, 'col_list': cols, 'value_list': values}
    )

Html 代码(试图访问值时卡住了):

table.html
{% extends "base.html" %}

{% block content %}
    <h2>My Table</h2>
    <ul>
        <TABLE border=4 bgcolor=lightgray align=center>
            <TR>
                <TD align=center bgcolor=darkgray><strong></strong></TD>

                {% for col in col_list %}
                <TD align=center bgcolor=darkgray><strong>{{ col.name }}</strong></TD>
                {% endfor %}
            </TR>

            {% for row in row_list %}
            <TR>
                <TD align=center bgcolor=darkgray><strong>{{ col.name }}</strong></TD>

                {% for col in col_list %}
                {% if row in col.rows.all %}
                <!-- How can I access the table values here? -->
                <TD align=left><input type="text" name="value" value="???" id="id_{{ col.id }}_{{ row.id }}" /></TD>
                {% else %}
                <TD align=left><input type="text" name="value" value="" id="{{col.id}}_{{row.id}}" /></TD>
                {% endif %}
                {% endfor %}
            </TR>
            {% endfor %}
    </ul>

{% endblock %}

我最终想让它成为一个可以更新值的表单,但现在我只需要让我的 html 代码正常工作。任何帮助将不胜感激。

【问题讨论】:

    标签: django


    【解决方案1】:

    您应该使用您的值创建一个二维数组:

        col_lookup = dict([(col.id, i) for i, col in enumerate(cols)])
    
        row_lookup = dict([(row.id, i) for i, row in enumerate(rows)])
    
        # create an "empty" two dimensional array.
        # each row is a tuple in the format (row, [False, False, False...])
        table = [(row, [False for x in xrange(len(col_lookup))]) for row in rows]
    
        for cell in Table.objects.all():
           table[row_lookup[cell.row_id]][1][col_lookup[cell.col_id]] = cell.value
    

    现在渲染table。使用{%for row, values in table%}...{{row.name}}...{%for value in values%}

    (我没有测试代码。建议:将你的Table模型重命名为Cell

    【讨论】:

      猜你喜欢
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 2020-06-18
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 2019-10-16
      相关资源
      最近更新 更多