Context Variables
Context Variables¶
Context variables are provided to each template when it is rendered. The available context variables depend on the model type for which the template is being rendered.
Global Context¶
In addition to the model-specific context variables, the following global context variables are available to all templates:
| Variable | Type | Description |
|---|---|---|
| base_url | str |
The base URL for the InvenTree instance |
| date | datetime.date |
Current date, represented as a Python datetime.date object |
| datetime | datetime.datetime |
Current datetime, represented as a Python datetime object |
| template | report.models.ReportTemplateBase |
The report template instance which is being rendered against |
| template_description | str |
Description of the report template |
| template_name | str |
Name of the report template |
| template_revision | int |
Revision of the report template |
| user | typing.Union[django.contrib.auth.models.AbstractUser, None] |
User who made the request to render the template |
Return base context data (available to all templates).
Source code in src/backend/InvenTree/report/models.py
323 324 325 326 327 328 329 330 331 332 333 334 | |
Report Context¶
In addition to the global context, all report templates have access to the following context variables:
| Variable | Type | Description |
|---|---|---|
| page_size | str |
The page size of the report |
| landscape | bool |
Boolean value, True if the report is in landscape mode |
| merge | bool |
Boolean value, True if the a single report is generated against multiple items |
When using the merge context variable, the selected items are available in the instances list. ??? abstract "Template: inventree_stock_report_merge.html"
```html
{% extends "report/inventree_report_base.html" %}
{% load i18n %}
{% load report %}
{% load inventree_extras %}
{% block style %}
.test-table {
width: 100%;
}
{% block bottom_left %}
content: "{% format_date date %}";
{% endblock bottom_left %}
{% block bottom_center %}
content: "{% inventree_version shortstring=True %}";
{% endblock bottom_center %}
{% block top_center %}
content: "{% trans 'Stock Item Test Report' %}";
{% endblock top_center %}
.test-row {
padding: 3px;
}
.test-pass {
color: #5f5;
}
.test-fail {
color: #F55;
}
.test-not-found {
color: #33A;
}
.required-test-not-found {
color: #EEE;
background-color: #F55;
}
.container {
padding: 5px;
border: 1px solid;
}
.text-left {
display: inline-block;
width: 50%;
}
.img-right {
display: inline;
align-content: right;
align-items: right;
width: 50%;
}
.part-img {
height: 4cm;
}
{% endblock style %}
{% block pre_page_content %}
{% endblock pre_page_content %}
{% block page_content %}
{% for item in instances %}
<div class='container'>
<div class='text-left'>
<h2>
{{ item.part.full_name }}
</h2>
<p>{{ item.part.description }}</p>
<p><em>{{ item.stock_item.location }}</em></p>
<p><em>Stock Item ID: {{ item.stock_item.pk }}</em></p>
</div>
<div class='img-right'>
<img class='part-img' alt='{% trans "Part image" %}' src="{% part_image item.part height=480 %}">
<hr>
<h4>
{% if item.stock_item.is_serialized %}
{% trans "Serial Number" %}: {{ item.stock_item.serial }}
{% else %}
{% trans "Quantity" %}: {% decimal item.stock_item.quantity %}
{% endif %}
</h4>
</div>
</div>
{% if item.installed_items|length > 0 %}
<h3>{% trans "Installed Items" %}</h3>
<table class='table test-table'>
<thead>
</thead>
<tbody>
{% for sub_item in item.installed_items %}
<tr>
<td>
<img src='{% part_image sub_item.part height=240 %}' class='part-img' alt='{% trans "Part image" %}' style='max-width: 24px; max-height: 24px;'>
{{ sub_item.part.full_name }}
</td>
<td>
{% if sub_item.serialized %}
{% trans "Serial" %}: {{ sub_item.serial }}
{% else %}
{% trans "Quantity" %}: {% decimal sub_item.quantity %}
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% endfor %}
{% endblock page_content %}
{% block post_page_content %}
{% endblock post_page_content %}
```
shows a complete example. To access individual item attributes, you can either loop through the instances or access them by index like instance.0.name.
Below is an example template that generates a single report for some selected parts. Each part occupies a row in the table
<h2>Merged Report for Selected Parts</h2>
<table>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
{% for part in instances %}
<tr>
<td>{{ part.name }}</td>
<td>{{ part.description }}</td>
</tr>
{% endfor %}
</table>
Note that custom plugins may also add additional context variables to the report context.
Supply context data to the report template for rendering.
Source code in src/backend/InvenTree/report/models.py
423 424 425 426 427 428 429 430 431 432 | |
Label Context¶
In addition to the global context, all label templates have access to the following context variables:
| Variable | Type | Description |
|---|---|---|
| width | float |
The width of the label (in mm) |
| height | float |
The height of the label (in mm) |
| page_style | typing.Union[str, None] |
The CSS @page style for the label template. This is used to be inserted at the top of the style block for a given label |
Note that custom plugins may also add additional context variables to the label context.
Supply context data to the label template for rendering.
Source code in src/backend/InvenTree/report/models.py
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 | |
Template Types¶
Templates (whether for generating reports or labels) are rendered against a particular "model" type. The following model types are supported, and can have templates renderer against them:
| Model Type | Description |
|---|---|
| company | A Company instance |
| build | A Build Order instance |
| buildline | A Build Order Line Item instance |
| salesorder | A Sales Order instance |
| returnorder | A Return Order instance |
| purchaseorder | A Purchase Order instance |
| stockitem | A StockItem instance |
| stocklocation | A StockLocation instance |
| part | A Part instance |
Company¶
When printing a report or label against a Company instance, the following context variables are available:
| Variable | Type | Description |
|---|---|---|
| company | company.models.Company |
The Company object associated with this context |
| name | str |
The name of the Company |
| description | str |
A description of the Company |
| website | str |
The website URL for the Company |
| phone | str |
The contact phone number for the Company |
str |
The contact email address for the Company | |
| address | str |
The primary address associated with the Company |
Generate a dict of context data to provide to the reporting framework.
Source code in src/backend/InvenTree/company/models.py
130 131 132 133 134 135 136 137 138 139 140 | |
Build Order¶
When printing a report or label against a Build Order object, the following context variables are available:
| Variable | Type | Description |
|---|---|---|
| bom_items | QuerySet[part.models.BomItem] |
Query set of all BuildItem objects associated with the BuildOrder |
| build | build.models.Build |
The BuildOrder instance itself |
| build_outputs | QuerySet[stock.models.StockItem] |
Query set of all BuildItem objects associated with the BuildOrder |
| line_items | QuerySet[build.models.BuildLine] |
Query set of all build line items associated with the BuildOrder |
| part | part.models.Part |
The Part object which is being assembled in the build order |
| quantity | int |
The total quantity of the part being assembled |
| reference | str |
The reference field of the BuildOrder |
| title | str |
The title field of the BuildOrder |
Generate custom report context data.
Source code in src/backend/InvenTree/build/models.py
221 222 223 224 225 226 227 228 229 230 231 232 | |
Build Line¶
When printing a report or label against a BuildOrderLineItem object, the following context variables are available:
| Variable | Type | Description |
|---|---|---|
| allocated_quantity | decimal.Decimal |
The quantity of the part which has been allocated to this build |
| allocations | QuerySet[build.models.BuildItem] |
A query set of all StockItem objects which have been allocated to this build line |
| bom_item | part.models.BomItem |
The BomItem associated with this line item |
| build | build.models.Build |
The BuildOrder instance associated with this line item |
| build_line | build.models.BuildLine |
The build line instance itself |
| part | part.models.Part |
The sub-part (component) associated with the linked BomItem instance |
| quantity | decimal.Decimal |
The quantity required for this line item |
Generate custom report context for this BuildLine object.
Source code in src/backend/InvenTree/build/models.py
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 | |
Sales Order¶
When printing a report or label against a SalesOrder object, the following context variables are available:
| Variable | Type | Description |
|---|---|---|
| description | str |
The description field of the SalesOrder |
| reference | str |
The reference field of the SalesOrder |
| title | str |
The title (string representation) of the SalesOrder |
| extra_lines | QuerySet[order.models.SalesOrderExtraLine] |
Query set of all extra lines associated with the SalesOrder |
| lines | QuerySet[order.models.SalesOrderLineItem] |
Query set of all line items associated with the SalesOrder |
| order | order.models.SalesOrder |
The SalesOrder instance itself |
| customer | typing.Union[company.models.Company, None] |
The customer object associated with the SalesOrder |
Generate context data for the reporting interface.
Source code in src/backend/InvenTree/order/models.py
404 405 406 407 408 409 410 411 412 413 | |
Sales Order Shipment¶
When printing a report or label against a SalesOrderShipment object, the following context variables are available:
| Variable | Type | Description |
|---|---|---|
| allocations | QuerySet[order.models.SalesOrderAllocation] |
QuerySet of SalesOrderAllocation objects |
| order | order.models.SalesOrder |
The associated SalesOrder object |
| reference | str |
Shipment reference string |
| address | company.models.Address |
The shipping address for this shipment (or order) |
| shipment | order.models.SalesOrderShipment |
The SalesOrderShipment object itself |
| tracking_number | str |
Shipment tracking number string |
| title | str |
Title for the report |
Generate context data for the reporting interface.
Source code in src/backend/InvenTree/order/models.py
2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 | |
Return Order¶
When printing a report or label against a ReturnOrder object, the following context variables are available:
| Variable | Type | Description |
|---|---|---|
| description | str |
The description field of the ReturnOrder |
| reference | str |
The reference field of the ReturnOrder |
| title | str |
The title (string representation) of the ReturnOrder |
| extra_lines | QuerySet[order.models.ReturnOrderExtraLine] |
Query set of all extra lines associated with the ReturnOrder |
| lines | QuerySet[order.models.ReturnOrderLineItem] |
Query set of all line items associated with the ReturnOrder |
| order | order.models.ReturnOrder |
The ReturnOrder instance itself |
| customer | typing.Union[company.models.Company, None] |
The customer object associated with the ReturnOrder |
Purchase Order¶
When printing a report or label against a PurchaseOrder object, the following context variables are available:
| Variable | Type | Description |
|---|---|---|
| description | str |
The description field of the PurchaseOrder |
| reference | str |
The reference field of the PurchaseOrder |
| title | str |
The title (string representation) of the PurchaseOrder |
| extra_lines | QuerySet[order.models.PurchaseOrderExtraLine] |
Query set of all extra lines associated with the PurchaseOrder |
| lines | QuerySet[order.models.PurchaseOrderLineItem] |
Query set of all line items associated with the PurchaseOrder |
| order | order.models.PurchaseOrder |
The PurchaseOrder instance itself |
| supplier | typing.Union[company.models.Company, None] |
The supplier object associated with the PurchaseOrder |
Stock Item¶
When printing a report or label against a StockItem object, the following context variables are available:
| Variable | Type | Description |
|---|---|---|
| barcode_data | str |
Generated barcode data for the StockItem |
| barcode_hash | str |
Hash of the barcode data |
| batch | str |
The batch code for the StockItem |
| child_items | QuerySet[stock.models.StockItem] |
Query set of all StockItem objects which are children of this StockItem |
| ipn | types.UnionType[str, None] |
The IPN (internal part number) of the associated Part |
| installed_items | set[stock.models.StockItem] |
Query set of all StockItem objects which are installed in this StockItem |
| item | stock.models.StockItem |
The StockItem object itself |
| name | str |
The name of the associated Part |
| part | part.models.Part |
The Part object which is associated with the StockItem |
| qr_data | str |
Generated QR code data for the StockItem |
| qr_url | str |
Generated URL for embedding in a QR code |
| parameters | dict[str, str] |
Dict object containing the parameters associated with the base Part |
| quantity | decimal.Decimal |
The quantity of the StockItem |
| result_list | list[stock.models.StockItemTestResult] |
FLattened list of TestResult data associated with the stock item |
| results | dict[str, stock.models.StockItemTestResult] |
Dict object of TestResult data associated with the StockItem |
| serial | types.UnionType[str, None] |
The serial number of the StockItem |
| stock_item | stock.models.StockItem |
The StockItem object itself (shadow of 'item') |
| tests | dict[str, stock.models.StockItemTestResult] |
Dict object of TestResult data associated with the StockItem (shadow of 'results') |
| test_keys | list[str] |
List of test keys associated with the StockItem |
| test_template_list | QuerySet[part.models.PartTestTemplate] |
List of test templates associated with the StockItem |
| test_templates | dict[str, part.models.PartTestTemplate] |
Dict object of test templates associated with the StockItem |
Generate custom report context data for this StockItem.
Source code in src/backend/InvenTree/stock/models.py
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 | |
Stock Location¶
When printing a report or label against a StockLocation object, the following context variables are available:
| Variable | Type | Description |
|---|---|---|
| location | stock.models.StockLocation |
The StockLocation object itself |
| qr_data | str |
Formatted QR code data for the StockLocation |
| parent | types.UnionType[stock.models.StockLocation, None] |
The parent StockLocation object |
| stock_location | stock.models.StockLocation |
The StockLocation object itself (shadow of 'location') |
| stock_items | QuerySet[stock.models.StockItem] |
Query set of all StockItem objects which are located in the StockLocation |
Return report context data for this StockLocation.
Source code in src/backend/InvenTree/stock/models.py
169 170 171 172 173 174 175 176 177 | |
Part¶
When printing a report or label against a Part object, the following context variables are available:
| Variable | Type | Description |
|---|---|---|
| bom_items | QuerySet[part.models.BomItem] |
Query set of all BomItem objects associated with the Part |
| category | types.UnionType[part.models.PartCategory, None] |
The PartCategory object associated with the Part |
| description | str |
The description field of the Part |
| IPN | types.UnionType[str, None] |
The IPN (internal part number) of the Part |
| name | str |
The name of the Part |
| parameters | dict[str, str] |
Dict object containing the parameters associated with the Part |
| part | part.models.Part |
The Part object itself |
| qr_data | str |
Formatted QR code data for the Part |
| qr_url | str |
Generated URL for embedding in a QR code |
| revision | types.UnionType[str, None] |
The revision of the Part |
| test_template_list | QuerySet[part.models.PartTestTemplate] |
List of test templates associated with the Part |
| test_templates | dict[str, part.models.PartTestTemplate] |
Dict object of test templates associated with the Part |
Return custom report context information.
Source code in src/backend/InvenTree/part/models.py
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | |
Model Variables¶
Additional to the context variables provided directly to each template, each model type has a number of attributes and methods which can be accessedd via the template.
For each model type, a subset of the most commonly used attributes are listed below. For a full list of attributes and methods, refer to the source code for the particular model type.
Parts¶
Part¶
Each part object has access to a lot of context variables about the part. The following context variables are provided when accessing a Part object from within the template.
| Variable | Description |
|---|---|
| name | Brief name for this part |
| full_name | Full name for this part (including IPN, if not null and including variant, if not null) |
| variant | Optional variant number for this part - Must be unique for the part name |
| category | The PartCategory object to which this part belongs |
| description | Longer form description of the part |
| keywords | Optional keywords for improving part search results |
| IPN | Internal part number (optional) |
| revision | Part revision |
| is_template | If True, this part is a 'template' part |
| link | Link to an external page with more information about this part (e.g. internal Wiki) |
| image | Image of this part |
| default_location | The default StockLocation object where the item is normally stored (may be null) |
| default_supplier | The default SupplierPart which should be used to procure and stock this part |
| default_expiry | The default expiry duration for any StockItem instances of this part |
| minimum_stock | Minimum preferred quantity to keep in stock |
| units | Units of measure for this part (default='pcs') |
| salable | Can this part be sold to customers? |
| assembly | Can this part be build from other parts? |
| component | Can this part be used to make other parts? |
| purchaseable | Can this part be purchased from suppliers? |
| trackable | Trackable parts can have unique serial numbers assigned, etc, etc |
| active | Is this part active? Parts are deactivated instead of being deleted |
| virtual | Is this part "virtual"? e.g. a software product or similar |
| notes | Additional notes field for this part |
| creation_date | Date that this part was added to the database |
| creation_user | User who added this part to the database |
| responsible | User who is responsible for this part (optional) |
| starred | Whether the part is starred or not |
| disabled | Whether the part is disabled or not |
| total_stock | The total amount in stock |
| quantity_being_built | The amount being built |
| required_build_order_quantity | The amount required for build orders |
| allocated_build_order_quantity | The amount allocated for build orders |
| build_order_allocations | Query set with all build order allocations for that part |
| required_sales_order_quantity | The amount required for sales orders |
| allocated_sales_order_quantity | The amount allocated for sales orders |
| available | Whether the part is available or not |
| on_order | The amount that are on order |
| required | The total amount required for build orders and sales orders |
| allocated | The total amount allocated for build orders and sales orders |
Part Category¶
| Variable | Description |
|---|---|
| name | Name of this category |
| parent | Parent category |
| default_location | Default StockLocation object for parts in this category or child categories |
| default_keywords | Default keywords for parts created in this category |
Stock¶
StockItem¶
| Variable | Description |
|---|---|
| parent | Link to another StockItem from which this StockItem was created |
| uid | Field containing a unique-id which is mapped to a third-party identifier (e.g. a barcode) |
| part | Link to the master abstract Part that this StockItem is an instance of |
| supplier_part | Link to a specific SupplierPart (optional) |
| location | The StockLocation Where this StockItem is located |
| quantity | Number of stocked units |
| batch | Batch number for this StockItem |
| serial | Unique serial number for this StockItem |
| link | Optional URL to link to external resource |
| updated | Date that this stock item was last updated (auto) |
| expiry_date | Expiry date of the StockItem (optional) |
| stocktake_date | Date of last stocktake for this item |
| stocktake_user | User that performed the most recent stocktake |
| review_needed | Flag if StockItem needs review |
| delete_on_deplete | If True, StockItem will be deleted when the stock level gets to zero |
| status | Status of this StockItem (ref: InvenTree.status_codes.StockStatus) |
| status_label | Textual representation of the status e.g. "OK" |
| notes | Extra notes field |
| build | Link to a Build (if this stock item was created from a build) |
| is_building | Boolean field indicating if this stock item is currently being built (or is "in production") |
| purchase_order | Link to a PurchaseOrder (if this stock item was created from a PurchaseOrder) |
| infinite | If True this StockItem can never be exhausted |
| sales_order | Link to a SalesOrder object (if the StockItem has been assigned to a SalesOrder) |
| purchase_price | The unit purchase price for this StockItem - this is the unit price at time of purchase (if this item was purchased from an external supplier) |
| packaging | Description of how the StockItem is packaged (e.g. "reel", "loose", "tape" etc) |
StockLocation¶
| Variable | Description |
|---|---|
| barcode | Brief payload data (e.g. for labels). Example: {"stocklocation": 826} where 826 is the primary key |
| description | The description of the location |
| icon | The name of the icon if set, e.g. fas fa-warehouse |
| item_count | Simply returns the number of stock items in this location |
| name | The name of the location. This is only the name of this location, not the path |
| owner | The owner of the location if it has one. The owner can only be assigned in the admin interface |
| parent | The parent location. Returns None if it is already the top most one |
| path | A queryset of locations that contains the hierarchy starting from the top most parent |
| pathstring | A string that contains all names of the path separated by slashes e.g. A/B/C |
| structural | True if the location is structural |
Suppliers¶
Company¶
| Variable | Description |
|---|---|
| name | Name of the company |
| description | Longer form description |
| website | URL for the company website |
| primary_address | Address object that is marked as primary address |
| address | String format of the primary address |
| contact | Contact Name |
| phone | Contact phone number |
| Contact email address | |
| link | A second URL to the company (Actually only accessible in the admin interface) |
| notes | Extra notes about the company (Actually only accessible in the admin interface) |
| is_customer | Boolean value, is this company a customer |
| is_supplier | Boolean value, is this company a supplier |
| is_manufacturer | Boolean value, is this company a manufacturer |
| currency_code | Default currency for the company |
| parts | Query set with all parts that the company supplies |
Address¶
| Variable | Description |
|---|---|
| line1 | First line of the postal address |
| line2 | Second line of the postal address |
| postal_code | ZIP code of the city |
| postal_city | City name |
| country | Country name |
Contact¶
| Variable | Description |
|---|---|
| company | Company object where the contact belongs to |
| name | First and second name of the contact |
| phone | Phone number |
| Email address | |
| role | Role of the contact |
SupplierPart¶
| Variable | Description |
|---|---|
| part | Link to the master Part (Obsolete) |
| source_item | The sourcing StockItem linked to this SupplierPart instance |
| supplier | Company that supplies this part |
| SKU | Stock keeping unit (supplier part number) |
| link | Link to external website for this supplier part |
| description | Descriptive notes field |
| note | Longer form note field |
| base_cost | Base charge added to order independent of quantity e.g. "Reeling Fee" |
| multiple | Multiple that the part is provided in |
| lead_time | Supplier lead time |
| packaging | packaging that the part is supplied in, e.g. "Reel" |
| pretty_name | The IPN, supplier name, supplier SKU and (if not null) manufacturer string joined by |. Ex. P00037 | Company | 000021 |
| unit_pricing | The price for one unit. |
| price_breaks | Return the associated price breaks in the correct order |
| has_price_breaks | Whether this SupplierPart has price breaks |
| manufacturer_string | Format a MPN string for this SupplierPart. Concatenates manufacture name and part number. |
User¶
| Variable | Description |
|---|---|
| username | the username of the user |
| fist_name | The first name of the user |
| last_name | The last name of the user |
| The email address of the user | |
| pk | The primary key of the user |