如何将两个模型中的django对象放入模板表并进行排序?
我正试图将“信息”放入一个表中,该表包含称为“类别”和“样式”的行和列。我的模型和视图如下所示如何将两个模型中的django对象放入模板表并进行排序?,django,html-table,django-templates,django-views,Django,Html Table,Django Templates,Django Views,我正试图将“信息”放入一个表中,该表包含称为“类别”和“样式”的行和列。我的模型和视图如下所示 model.py class Headings(models.Model): categories = models.CharField(max_length=20) stylings = models.CharField(max_length=20) class Information(models.Model): headings = models.ForeignKey
model.py
class Headings(models.Model):
categories = models.CharField(max_length=20)
stylings = models.CharField(max_length=20)
class Information(models.Model):
headings = models.ForeignKey(Headings)
info = models.CharField(max_length=100)
views.py
class InformationListView(ListView):
model = Information
def get_context_data(self, **kwargs):
context = super(InformationListView, self).get_context_data(**kwargs)
context['categories_ordered'] = Headings.objects.order_by('categories')
context['stylings_ordered'] = Headings.objects.order_by('stylings')
return context
到目前为止,我已经能够开始我的模板表
<table>
<tr>
<th>Styles</th>
{% for cols in categories_ordered %}
<th class="rotate"><div><span>{{ cols.categories }}</span></div>
{% endfor %}
</th>
</tr>
{% for row in stylings_ordered %}
<tr>
<td>{{ row.stylings }}</td>
{% for col in categories_ordered %}
<td>
... need algorithm that places the correct info in this cell. {{ Information.info }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
风格
{类别中COL的%u排序%}
{{cols.categories}}
{%endfor%}
{样式中的行为%u%}
{{row.styleings}}
{类别中列的%u已排序%}
... 需要在该单元格中放置正确信息的算法。{{Information.info}
{%endfor%}
{%endfor%}
可能有6个不同的类别和6种不同的样式,但可能只有4个信息对象。因此,最难的部分(imo)是将信息放在表中正确的单元格中。我尝试过不同的事情,但似乎没有什么是接近的。我想这是一个相当普遍的问题,一直都在解决
headings = models.ForeignKey(Headings)
信息的属性标题是标题的FK
<table>
<tr>
<th>Styles</th>
{% for cols in categories_ordered %}
<th class="rotate"><div><span>{{ cols.categories }}</span></div>
{% endfor %}
</th>
</tr>
{% for row in stylings_ordered %}
<tr>
<td>{{ row.stylings }}</td>
{% for col in row.information_set.all() %}
<td>col.info</td>
{% endfor %}
</tr>
{% endfor %}
</table>
我会使用JSONField。如果使用PostgresSQL,那么可以在DJango中使用JSONField
我不知道这是最好的方法,但这是我能想到的
# playground.html
<table >
<thead>
<tr>
<th>Styles</th>
{% for category in categories %}
<th>{{category.name}}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for style in styles %}
<tr>
<td>{{style.name}}</td>
{% for category in categories %}
<td>{{style.info|get_item:category.name}}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
是的,但这只是将信息从左到右全部放在一行中。信息不一定与标题一致。假设按以下顺序输入类别:运动、爱好、绘画、陶器、摄影、滑雪。假设只有一种样式。然后可能只有两个信息输入,一个用于绘画,一个用于滑雪。表格应跳过前两列,填写绘画,跳过陶器和摄影列,然后填写滑雪列。当行和列都必须匹配时,它变得更加复杂。请检查我的答案,这看起来就像我正在尝试做的。
#views.py
class PlayGround(View):
def get(self, request, *args, **kwargs):
categories = Categories.objects.all()
styles = Styles.objects.all()
return render(
request,
'playground.html',
{"categories": categories, "styles": styles}
)
# adding template filter to get value from key in template
from django.template.defaulttags import register
@register.filter
def get_item(dictionary, key):
return dictionary.get(key, '')
# playground.html
<table >
<thead>
<tr>
<th>Styles</th>
{% for category in categories %}
<th>{{category.name}}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for style in styles %}
<tr>
<td>{{style.name}}</td>
{% for category in categories %}
<td>{{style.info|get_item:category.name}}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
id 1
name "teststyle"
info "{"skiing": "info2", "painting": "info1"}"