How to place and order django objects from 2 models into template table? -
i'm trying place "information" table has rows , columns called "categories" , "stylings". model , view shown below.
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
so far i've been able start template table with
<table> <tr> <th>styles</th> {% cols in categories_ordered %} <th class="rotate"><div><span>{{ cols.categories }}</span></div> {% endfor %} </th> </tr> {% row in stylings_ordered %} <tr> <td>{{ row.stylings }}</td> {% col in categories_ordered %} <td> ... need algorithm places correct info in cell. {{ information.info }} </td> {% endfor %} </tr> {% endfor %} </table>
there 6 different categories , 6 different stylings, maybe 4 information objects. hard part (imo) placing information in correct cell in table. i've tried different things nothing seems close. imagine common problem solved time.
headings = models.foreignkey(headings)
attribute headings of information fk of headings.
<table> <tr> <th>styles</th> {% cols in categories_ordered %} <th class="rotate"><div><span>{{ cols.categories }}</span></div> {% endfor %} </th> </tr> {% row in stylings_ordered %} <tr> <td>{{ row.stylings }}</td> {% col in row.information_set.all() %} <td>col.info</td> {% endfor %} </tr> {% endfor %} </table>
you can check many 1 relationship in django manual in detail.
answer comment
class styles(models.model): name = models.charfield(max_length=20) info = jsonfield(default={}) class categories(models.model): name = models.charfield(max_length=20) styles = models.foreignkey(styles)
i use jsonfield. if use postgressql, can use jsonfield in django. json field
#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 value key in template django.template.defaulttags import register @register.filter def get_item(dictionary, key): return dictionary.get(key, '')
i don't know best way come with.
# playground.html <table > <thead> <tr> <th>styles</th> {% category in categories %} <th>{{category.name}}</th> {% endfor %} </tr> </thead> <tbody> {% style in styles %} <tr> <td>{{style.name}}</td> {% category in categories %} <td>{{style.info|get_item:category.name}}</td> {% endfor %} </tr> {% endfor %} </tbody> </table>
styles sample data
id 1 name "teststyle" info "{"skiing": "info2", "painting": "info1"}"
Comments
Post a Comment