How to get related field's value as ManyToMany fields' choice label in Django

余生长醉 提交于 2021-02-11 12:24:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!