2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-12 10:05:39 +00:00

Parameter by name (#5055)

* Add method get_parameter

- Return a parameter for a part, on name

* Add unit test for new method

* Adds template tag to retrieve parameter based on name

* Update docs
This commit is contained in:
Oliver
2023-06-16 12:14:17 +10:00
committed by GitHub
parent 51cece9e07
commit 31ff3599eb
6 changed files with 122 additions and 4 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -224,3 +224,31 @@ You can add asset images to the reports and labels by using the `{% raw %}{% ass
<img src="{% asset 'my_awesome_logo.png' %}"/>
{% endraw %}
```
## Part Parameters
If you need to load a part parameter for a particular Part, within the context of your template, you can use the `part_parameter` template tag.
The following example assumes that you have a report or label which contains a valid [Part](../part/part.md) instance:
```
{% raw %}
{% load report %}
{% part_parameter part "length" as length %}
Part: {{ part.name }}<br>
Length: {{ length.data }} [{{ length.units }}]
{% endraw %}
```
A [Part Parameter](../part/parameter.md) has the following available attributes:
| Attribute | Description |
| --- | --- |
| Name | The *name* of the parameter (e.g. "Length") |
| Description | The *description* of the parameter |
| Data | The *value* of the parameter (e.g. "123.4") |
| Units | The *units* of the parameter (e.g. "km") |
| Template | A reference to a [PartParameterTemplate](../part/parameter.md#parameter-templates) |

View File

@ -34,9 +34,9 @@ The following context variables are made available to the Part label template:
| qr_data | String data which can be rendered to a QR code |
| parameters | Map (Python dictionary) object containing the parameters associated with the part instance |
#### Parameters
#### Parameter Values
The part parameters can be accessed by parameter name lookup in the template, as follows:
The part parameter *values* can be accessed by parameter name lookup in the template, as follows:
```html
{% raw %}
@ -47,7 +47,8 @@ Length: {{ parameters.length }}
{% endraw %}
```
Note that for parameters which include a `space` character in their name, lookup using the "dot" notation won't work! In this case, try using the [key lookup](../helpers.md#key-access) method:
!!! warning "Spaces"
Note that for parameters which include a `space` character in their name, lookup using the "dot" notation won't work! In this case, try using the [key lookup](../helpers.md#key-access) method:
```html
{% raw %}
@ -55,3 +56,36 @@ Note that for parameters which include a `space` character in their name, lookup
Voltage Rating: {% getkey parameters "Voltage Rating" %}
{% endraw %}
```
#### Parameter Data
If you require access to the parameter data itself, and not just the "value" of a particular parameter, you can use the `part_parameter` [helper function](../helpers.md#part-parameters).
For example, the following label template can be used to generate a label which contains parameter data in addition to parameter units:
```html
{% raw %}
{% extends "label/label_base.html" %}
{% load report %}
{% block content %}
{% part_parameter part "Width" as width %}
{% part_parameter part "Length" as length %}
<div>
Part: {{ part.full_name }}<br>
Width: {{ width.data }} [{{ width.units }}]<br>
Length: {{ length.data }} [{{ length.units }}]
</div>
{% endblock content %}
{% endraw %}
```
The following label is produced:
{% with id="report-parameters", url="report/label_with_parameters.png", description="Label with parameters" %}
{% include 'img.html' %}
{% endwith %}