如何处理具有浮动内联表的场景,该内联表是预先存在的数据库中的维度,无论如何都与用户无关?目标是将此表加载到模板中的选择选项中,然后在提交时将保存发布到数据库。因此,在我的模型中,从请求模型到Table1模型,请求与表1之间将存在关系。
模特:
class Table1(models.Model):
field1 = models.CharField(db_column='Unit_Num', max_length=5, blank=True, null=True)
field2 = models.CharField(db_column='Company_Code', max_length=1, blank=True, null=True)
class Meta:
managed = False
db_table = 'Table1'
我不确定的观点,但我认为它会是:
table1 = Table1.objects.all()
args = {'table1':table1}
return render(request, 'accounts/profile.html', args)
现在填充选择我在模板中尝试了以下内容,但它只是放了一堆空框:
{% for table in table1 %}
<select table1select="Choose a Option..." class="chosen-select" multiple tabindex="4">
<option value="{{table1.field1}}"></option>
</select>
{% endfor %}
我猜它是空的,因为与用户没有任何关系,但我只想列出数据库中field1的每一行。
我添加了我所看到的屏幕截图:
答案 0 :(得分:1)
您的问题是您应该从表变量而不是field1
访问table1
,所以我想这是正确的代码:
<select table1select="Choose a Option..." class="chosen-select" multiple tabindex="4">
{% for table in table1 %}
<option value="{{table.field1}}">{{table.field1}}</option>
{% endfor %}
</select>