Как отобразить тройные вложенные значения словаря в шаблоне Django? - PullRequest
0 голосов
/ 10 апреля 2019

У меня есть список словарей, созданных с помощью необработанного запроса django, и пользовательский алгоритм сортировки:

ordered_list = [{'qmaLevel': Decimal('1'), 'materials': None, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': '12U x 600 x 650 Professional Rack ', 'qmaQuoteAssemblyID': 0, 'qmaPartID': 'RACK S9 ', 'quantity': Decimal('1.00000'), 'qmaParentAssemblyID': 0}, 
{'qmaLevel': Decimal('2'), 'indent': '____', 'materials': None, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': '. ', 'qmaQuoteAssemblyID': 1, 'qmaPartID': 'COLOUR ', 'quantity': 1.0, 'qmaParentAssemblyID': 0}, 
{'qmaLevel': Decimal('3'), 'indent': '________', 'materials': None, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': 'Colour: Mannex Suede ', 'qmaQuoteAssemblyID': 21, 'qmaPartID': '800-560192 ', 'quantity': 1.0, 'qmaParentAssemblyID': 1}, 
{'qmaLevel': Decimal('2'), 'indent': '____', 'materials': None, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': '. ', 'qmaQuoteAssemblyID': 2, 'qmaPartID': 'FRAME ', 'quantity': 1.0, 'qmaParentAssemblyID': 0}, 
{'qmaLevel': Decimal('3'), 'indent': '________', 'materials': {1: {'qmmPartID': '128114 ', 'qmmPartShortDescription': '1.30;300;200;Black/White;Acrylic;Label; ', 'imrWeight': Decimal('0.00000')}, 2: {'qmmPartID': '800-560271 ', 'qmmPartShortDescription': 'Rivet;Alum;Truss;4.0;8.7; ', 'imrWeight': Decimal('0.00149')}, 3: {'qmmPartID': '800-560350 ', 'qmmPartShortDescription': 'Key;Ring;20mm; ', 'imrWeight': Decimal('0.00000')}}, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': 'Label 19" Rack Container Commercial ', 'qmaQuoteAssemblyID': 22, 'qmaPartID': '1A000 ', 'quantity': 1.0, 'qmaParentAssemblyID': 2}, 
{'qmaLevel': Decimal('3'), 'indent': '________', 'materials': {1: {'qmmPartID': '800-560008 ', 'qmmPartShortDescription': 'Colour;Powdercoat;General;Production Use ', 'imrWeight': Decimal('1.00000')}, 2: {'qmmPartID': '124632 ', 'qmmPartShortDescription': '1.90;M2;ZAN; ', 'imrWeight': Decimal('14.91500')}}, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': 'Frame Top x 600 x 650 Typ 0P ', 'qmaQuoteAssemblyID': 23, 'qmaPartID': '0R000P ', 'quantity': 1.0, 'qmaParentAssemblyID': 2}, 
{'qmaLevel': Decimal('3'), 'indent': '________', 'materials': {1: {'qmmPartID': '124632 ', 'qmmPartShortDescription': '1.90;M2;ZAN; ', 'imrWeight': Decimal('14.91500')}, 2: {'qmmPartID': '800-560008 ', 'qmmPartShortDescription': 'Colour;Powdercoat;General;Production Use ', 'imrWeight': Decimal('1.00000')}}, 'qmaQuantityPerParent': Decimal('4.00000'), 'qmaPartShortDescription': 'Frame Vertical 12U H2 Type 1 ', 'qmaQuoteAssemblyID': 24, 'qmaPartID': '0S0221 ', 'quantity': 4.0, 'qmaParentAssemblyID': 2}, 
{'qmaLevel': Decimal('3'), 'indent': '________', 'materials': {1: {'qmmPartID': '800-560008 ', 'qmmPartShortDescription': 'Colour;Powdercoat;General;Production Use ', 'imrWeight': Decimal('1.00000')}, 2: {'qmmPartID': '124632 ', 'qmmPartShortDescription': '1.90;M2;ZAN; ', 'imrWeight': Decimal('14.91500')}}, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': 'Frame Bottom x 600 x 650 Type 0P ', 'qmaQuoteAssemblyID': 25, 'qmaPartID': '0T000P ', 'quantity': 1.0, 'qmaParentAssemblyID': 2}, 
{'qmaLevel': Decimal('2'), 'indent': '____', 'materials': {1: {'qmmPartID': '800-560008 ', 'qmmPartShortDescription': 'Colour;Powdercoat;General;Production Use ', 'imrWeight': Decimal('1.00000')}}, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': '. ', 'qmaQuoteAssemblyID': 3, 'qmaPartID': 'MOUNTING ', 'quantity': 1.0, 'qmaParentAssemblyID': 0}, 
{'qmaLevel': Decimal('3'), 'indent': '________', 'materials': None, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': 'Base Pro. Levelling Feet + Mount Plate Kit GA ', 'qmaQuoteAssemblyID': 27, 'qmaPartID': '0E220 ', 'quantity': 1.0, 'qmaParentAssemblyID': 3}, 
{'qmaLevel': Decimal('4'), 'indent': '____________', 'materials': {1: {'qmmPartID': '800-560349 ', 'qmmPartShortDescription': 'M10;40;Levelling Foot Adjust Screw; ', 'imrWeight': Decimal('0.03081')}, 2: {'qmmPartID': '800-560268 ', 'qmmPartShortDescription': 'M16;Zinc;Hex;Locknut ', 'imrWeight': Decimal('0.00000')}, 3: {'qmmPartID': '800-560097 ', 'qmmPartShortDescription': 'M05;Zinc;Flange;Nut; ', 'imrWeight': Decimal('0.00164')}, 4: {'qmmPartID': '800-560266 ', 'qmmPartShortDescription': 'M10;Zinc;Hex;CLS8 ', 'imrWeight': Decimal('0.00905')}, 5: {'qmmPartID': '128263 ', 'qmmPartShortDescription': 'M16;M10;M24;Mild;Hex;Threaded;Bush;22mm; ', 'imrWeight': Decimal('0.00000')}, 6: {'qmmPartID': '800-560098 ', 'qmmPartShortDescription': 'M05;15;Steel ', 'imrWeight': Decimal('0.00000')}}, 'qmaQuantityPerParent': Decimal('4.00000'), 'qmaPartShortDescription': 'Levelling Feet ', 'qmaQuoteAssemblyID': 39, 'qmaPartID': '800-250215 ', 'quantity': 4.0, 'qmaParentAssemblyID': 27}, 
{'qmaLevel': Decimal('5'), 'indent': '________________', 'materials': {1: {'qmmPartID': '800-560098 ', 'qmmPartShortDescription': 'M05;15;Steel ', 'imrWeight': Decimal('0.00000')}, 2: {'qmmPartID': '124632 ', 'qmmPartShortDescription': '1.90;M2;ZAN; ', 'imrWeight': Decimal('14.91500')}}, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': ';;;;;Leveling;Foot;Plate; ', 'qmaQuoteAssemblyID': 40, 'qmaPartID': '800-250137 ', 'quantity': 4.0, 'qmaParentAssemblyID': 39}, 
{'qmaLevel': Decimal('3'), 'indent': '________', 'materials': {1: {'qmmPartID': '124887 ', 'qmmPartShortDescription': 'Rivet;Steel;Dome;4.8;1.6-6.4; ', 'imrWeight': Decimal('0.00000')}, 2: {'qmmPartID': '800-560013 ', 'qmmPartShortDescription': 'M06;Nutsert;Open;Large;Flange; ', 'imrWeight': Decimal('0.00000')}, 3: {'qmmPartID': '800-560014 ', 'qmmPartShortDescription': 'M06;16;Zinc;Phillips;Hex;Serrated ', 'imrWeight': Decimal('0.00502')}}, 'qmaQuantityPerParent': Decimal('4.00000'), 'qmaPartShortDescription': 'Gusset Mount Rail 12U x 600 ', 'qmaQuoteAssemblyID': 28, 'qmaPartID': '03020D0 ', 'quantity': 4.0, 'qmaParentAssemblyID': 3}, 
{'qmaLevel': Decimal('4'), 'indent': '____________', 'materials': {1: {'qmmPartID': '124633 ', 'qmmPartShortDescription': '1.50;M2;ZAN; ', 'imrWeight': Decimal('11.77500')}, 2: {'qmmPartID': '800-560008 ', 'qmmPartShortDescription': 'Colour;Powdercoat;General;Production Use ', 'imrWeight': Decimal('1.00000')}}, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': 'Mount Rail Gusset 12U Universal ', 'qmaQuoteAssemblyID': 41, 'qmaPartID': '127040 ', 'quantity': 4.0, 'qmaParentAssemblyID': 28}, 
{'qmaLevel': Decimal('4'), 'indent': '____________', 'materials': {1: {'qmmPartID': '124632 ', 'qmmPartShortDescription': '1.90;M2;ZAN; ', 'imrWeight': Decimal('14.91500')}, 2: {'qmmPartID': '800-560008 ', 'qmmPartShortDescription': 'Colour;Powdercoat;General;Production Use ', 'imrWeight': Decimal('1.00000')}}, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': '12U;0600;;;C;Mount;Rail; ', 'qmaQuoteAssemblyID': 42, 'qmaPartID': '127014 ', 'quantity': 4.0, 'qmaParentAssemblyID': 28}, 
{'qmaLevel': Decimal('2'), 'indent': '____', 'materials': None, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': '. ', 'qmaQuoteAssemblyID': 4, 'qmaPartID': 'FRONT DOOR ', 'quantity': 1.0, 'qmaParentAssemblyID': 0}, 
{'qmaLevel': Decimal('3'), 'indent': '________', 'materials': {1: {'qmmPartID': '800-560008 ', 'qmmPartShortDescription': 'Colour;Powdercoat;General;Production Use ', 'imrWeight': Decimal('1.00000')}}, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': 'No Door Required ', 'qmaQuoteAssemblyID': 29, 'qmaPartID': 'NO DOOR ', 'quantity': 1.0, 'qmaParentAssemblyID': 4}, 
{'qmaLevel': Decimal('2'), 'indent': '____', 'materials': None, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': '. ', 'qmaQuoteAssemblyID': 5, 'qmaPartID': 'REAR DOOR ', 'quantity': 1.0, 'qmaParentAssemblyID': 0}, 
{'qmaLevel': Decimal('3'), 'indent': '________', 'materials': {1: {'qmmPartID': '800-560008 ', 'qmmPartShortDescription': 'Colour;Powdercoat;General;Production Use ', 'imrWeight': Decimal('1.00000')}}, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': 'No Door Required ', 'qmaQuoteAssemblyID': 34, 'qmaPartID': 'NO DOOR ', 'quantity': 1.0, 'qmaParentAssemblyID': 5}, 
{'qmaLevel': Decimal('2'), 'indent': '____', 'materials': None, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': '(when viewed from front of rack) ', 'qmaQuoteAssemblyID': 6, 'qmaPartID': 'LEFT SIDE PANEL ', 'quantity': 1.0, 'qmaParentAssemblyID': 0}, 
{'qmaLevel': Decimal('2'), 'indent': '____', 'materials': None, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': '(when viewed from front of rack) ', 'qmaQuoteAssemblyID': 7, 'qmaPartID': 'RIGHT SIDE PANEL ', 'quantity': 1.0, 'qmaParentAssemblyID': 0}, 
{'qmaLevel': Decimal('2'), 'indent': '____', 'materials': None, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': '. ', 'qmaQuoteAssemblyID': 8, 'qmaPartID': 'ACCESSORIES ', 'quantity': 1.0, 'qmaParentAssemblyID': 0}, 
{'qmaLevel': Decimal('2'), 'indent': '____', 'materials': None, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': '. ', 'qmaQuoteAssemblyID': 9, 'qmaPartID': 'PDU ', 'quantity': 1.0, 'qmaParentAssemblyID': 0}, 
{'qmaLevel': Decimal('2'), 'indent': '____', 'materials': None, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': '. ', 'qmaQuoteAssemblyID': 10, 'qmaPartID': 'FRONT VCM ', 'quantity': 1.0, 'qmaParentAssemblyID': 0}, 
{'qmaLevel': Decimal('2'), 'indent': '____', 'materials': None, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': '. ', 'qmaQuoteAssemblyID': 11, 'qmaPartID': 'REAR VCM ', 'quantity': 1.0, 'qmaParentAssemblyID': 0}, 
{'qmaLevel': Decimal('2'), 'indent': '____', 'materials': None, 'qmaQuantityPerParent': Decimal('1.00000'), 'qmaPartShortDescription': '. ', 'qmaQuoteAssemblyID': 12, 'qmaPartID': 'PACKAGING ', 'quantity': 1.0, 'qmaParentAssemblyID': 0}]

Каждый словарь имеет вложенный словарь с именем materials, а каждый словарь materials имеет еще несколько вложенных словарей с ключом qmmQuoteMaterialID, которые в этом примере будут от 1 до 6, но могут иметь любое число от 1 до n в зависимости от запрос. Пример:

{'qmaLevel': Decimal('4'), 'indent': '____________', 'materials': {1: {'qmmPartID': '800-560349 ', 'qmmPartShortDescription': 'M10;40;Levelling Foot Adjust Screw; ', 'imrWeight': Decimal('0.03081')}, 2: {'qmmPartID': '800-560268 ', 'qmmPartShortDescription': 'M16;Zinc;Hex;Locknut ', 'imrWeight': Decimal('0.00000')}, 3: {'qmmPartID': '800-560097 ', 'qmmPartShortDescription': 'M05;Zinc;Flange;Nut; ', 'imrWeight': Decimal('0.00164')}, 4: {'qmmPartID': '800-560266 ', 'qmmPartShortDescription': 'M10;Zinc;Hex;CLS8 ', 'imrWeight': Decimal('0.00905')}, 5: {'qmmPartID': '128263 ', 'qmmPartShortDescription': 'M16;M10;M24;Mild;Hex;Threaded;Bush;22mm; ', 'imrWeight': Decimal('0.00000')}, 6: {'qmmPartID': '800-560098 ', 'qmmPartShortDescription': 'M05;15;Steel ', 'imrWeight': Decimal('0.00000')}}, 'qmaQuantityPerParent': Decimal('4.00000'), 'qmaPartShortDescription': 'Levelling Feet ', 'qmaQuoteAssemblyID': 39, 'qmaPartID': '800-250215 ', 'quantity': 4.0, 'qmaParentAssemblyID': 27},

Мне нужно отобразить содержимое в словаре qmmQuoteMaterialID в шаблоне Django, но я не уверен, как это сделать. Вот мой текущий шаблон:

{% for qmaQuoteAssemblyID in ordered_list %}
  <tr>
    <td class="text-center text-muted">{{ qmaQuoteAssemblyID.qmaLevel }}</td>
    <td>
      {% autoescape off %}
        <span style="color:white;">{{ qmaQuoteAssemblyID.indent|escape }}</span>
      {% endautoescape %}
      {{ qmaQuoteAssemblyID.qmaPartID }}
    </td>
    <td class="text-center">{{ qmaQuoteAssemblyID.qmaQuoteAssemblyID }}</td>
    <td class="text-center">{{ qmaQuoteAssemblyID.qmaParentAssemblyID }}</td>
    <td>{{ qmaQuoteAssemblyID.qmaPartShortDescription }}</td>
    <td class="text-center">{{ qmaQuoteAssemblyID.qmaQuantityPerParent|floatformat:"2" }}</td>
    <td class="text-center">{{ qmaQuoteAssemblyID.quantity|floatformat:"2"}}</td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    {% if qmaQuoteAssemblyID.materials != None %}
      {% for qmmQuoteMaterialID in qmaQuoteAssemblyID.materials %}
        <tr>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td>{{ qmmQuoteMaterialID.qmmQuoteMaterialID }}</td>
          <td>{{ qmmQuoteMaterialID.qmmPartID }}</td>
          <td>{{ qmmQuoteMaterialID.qmmPartShortDescription }}</td>
          <td>{{ qmmQuoteMaterialID.imrWeight }}</td>
        </tr>
      {% endfor %}
    {% endif %}
  </tr>
{% endfor %}

На данный момент вышеприведенный шаблон выделяет правильное количество строк для родительского элемента, однако он ничего не отображает, так как я, кажется, неправильно вызываю связанные данные.

Ответы [ 2 ]

1 голос
/ 10 апреля 2019

Хорошо, похоже, вы забыли о мелких деталях доступа к упорядоченному списку. По умолчанию без предоставления какого-либо метода доступа к dict просто верните ключ. Что нужно сделать, итерировать его элементы:

{% if qmaQuoteAssemblyID.materials != None %}
  {% for key, qmmQuoteMaterialID in qmaQuoteAssemblyID.materials.items %}
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td>{{ qmmQuoteMaterialID.qmmQuoteMaterialID }}</td>
      <td>{{ qmmQuoteMaterialID.qmmPartID }}</td>
      <td>{{ qmmQuoteMaterialID.qmmPartShortDescription }}</td>
      <td>{{ qmmQuoteMaterialID.imrWeight }}</td>
    </tr>

Здесь вы можете прочитать больше о dict структуре данных, ее методе и методах зацикливания.

https://docs.python.org/3.4/tutorial/datastructures.html#dictionaries https://docs.python.org/3.4/library/stdtypes.html#typesmapping

0 голосов
/ 10 апреля 2019

Шаблон Django не может использовать переменную для поиска словаря, подобного этому.Я смог создать желаемое поведение с помощью специального фильтра. Читайте официальный документ здесь

Пример:

from django.template.defaulttags import register


@register.filter
def get_item(dictionary, key):
    return dictionary.get(key)

в <your_app_name>/templatetags/<awesome_name>.py

В шаблоне:

{% load get_item %}
{% for level1 in my_dict %}
    {{ my_dict|get_item:level1 }}
    {% for level2 in my_dict|get_item:level1 %}
        {{ my_dict|get_item:level1|get_item:level2 }}
    {% endfor %} 
{% endfor %}

Я не пробовал level2.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...