2017-07-28 33 views
0

我試圖將「信息」放入一個表格中,該表格有稱爲「類別」和「樣式」的行和列。下面顯示了我的模型和視圖。如何將2個模型中的django對象放置到模板表中?

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> 

可能有6個不同的類別和6分不同的兩種風格,但也許只有4信息的對象。所以困難的部分(imo)將信息放在表格中正確的單元格中。我嘗試了不同的東西,但似乎沒有接近。我想這是一個相當普遍的問題,一直在解決。

回答

1
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> 

You can check many to one relationship in Django manual in detail.


答案評論

你的意思呢? enter image description here

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) 

我會用JSONField。如果你使用PostgresSQL,你可以在DJango中使用JSONField。 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 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"}" 

enter image description here

+0

是的,但只是放在信息到表中從左至右所有一行。信息不一定與標題對齊。假設類別按以下順序輸入:運動,愛好,繪畫,陶器,攝影,滑雪。假設只有一種風格。那麼也許只有兩個信息輸入,一個是繪畫,一個是滑雪。表格應該跳過前兩列,填寫繪畫,跳過陶器和攝影專欄,然後填寫滑雪專欄。當行和列都必須匹配時,它變得更加複雜。 –

+0

請檢查我的回答請 – Jayground

+0

是的,這看起來像我想要做的。 –

相關問題