问题
I have two related models with one of the fields of a model having ManyToManyField
relationship with the other model like shown here:
Models
class Processes(models.Model):
x_process = models.CharField(primary_key=True, ...)
x_process_text = models.CharField(max_length=35, verbose_name='Short Desc')
class RelStatuses(models.Model):
rel_status = models.CharField(primary_key=True, ...)
rel_status_text = models.CharField(max_length=55, verbose_name='Short Desc')
app_process_compo = models.ManyToManyField(Processes, ...)
Now in the template I am trying to display multiple choices as checkbox
es like this:
Template
...
{% for field in form.visible_fields %} {# Looping thru' form fields #}
...
{% if field.name == "app_process_compo" %}
{% for m_choice in field %}
<label for="{{ m_choice.id_for_label }}">
{{ m_choice.choice_label }}
<span class="m_choice">{{ m_choice.tag }}</span>
</label>
{% endfor %}
...
With this I am getting check boxes with the primary field (x_process
) values (such as A, P, Q etc) as the field label (as shown in the image) below:
Now how do I have the checkbox
label as the related fields' value i.e. the values stored in field x_process_text
instead of the value stored in pk field x_process
.
来源:https://stackoverflow.com/questions/64862877/how-to-get-related-fields-value-as-manytomany-fields-choice-label-in-django