简体   繁体   中英

Django: access to attributes of custom model field from html template

I implemented a custom model field:

class CustomCharField(models.CharField):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.my_attr = "MY_ATTR"

Now I want to be able to display that additional attribute when I'm iterating over all model's fields in change_form_template :

{% block field_sets %}
    {% for fieldset in adminform %}
        {% for line in fieldset %}
            {% for field in line %}
                {{ field.field }}

                {% if field.field.my_attr %}
                    {{ field.field.my_attr }}
                {% endif %}

            {% endfor %}
        {% endfor %}
    {% endfor %}
{% endblock %}

Unfortunately my_attr is not visible. I was searching the proper method in the django docs but without any success. I was also trying to use the custom form_class (inheriting from django.forms.fields.CharField )

EDIT:

Ok, so I need both custom form field and model field. Given the following code, what should I change to make it working? (of course I ignore passing the attribute value in that example). Why my_attr from CustomFormField is still not accessible from the template?

class CustomFormField(fields.CharField):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.my_attr = "MY_ATTR2"

class CustomCharField(models.CharField):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.my_attr = "MY_ATTR"

    def formfield(self, **kwargs):
        defaults = {'form_class': CustomFormField}
        defaults.update(kwargs)
        return super().formfield(**defaults)

Ok, thanks to the advices from @AbdulAzizBarkat and @Guillaume I finally managed how to resolve the problem.

In order to expose attribute of the model field at the HTML template, it's needed to implement also custom form field and pass the attribute in default dict in formfield method:

from django.db import models
from django.forms import fields

class CustomFormField(fields.CharField):
    def __init__(self, my_attr, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.my_attr = my_attr


class CustomCharField(models.CharField):
    def __init__(self, my_attr, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.my_attr = my_attr

    def formfield(self, **kwargs):
        defaults = {'form_class': CustomFormField, "my_attr": self.my_attr}
        defaults.update(kwargs)
        return super().formfield(**defaults)

Another change is the way of accessing to the attribute in the HTML template specified by setting change_form_template value. In that case it should be {{ field.field.field.my_attr }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM