How to reference the base model of a django model form object?

When creating a form, I want to use one field in the model as a label for navigating with another updated field.

I overridden BaseModelFormSet with the new ___init____ method, for example:

class BaseMyFormSet(BaseModelFormSet):
    def __init__(self, *args, **kwargs):
       super(BaseMyFormSet, self).__init__(*args, **kwargs)
    for form in self.forms:
           form.fields['value'].label = ???

How can I refer to another field in the model so that I can use it as a label value?

(Alternatively, if there is a better way to redefine the shortcut as I need, this is also very useful.)

Thanks.

+3
source share
2 answers

I don’t quite understand your goal: are you trying to use the field value for a specific model instance or are you trying to use your own model name or the help_text attribute from the model definition?

, . , , , init:

for form in self.forms:
    opts = self.model._meta
    field = opts.get_field("yourfield")
    form.fields['value'].label = field.name

form.fields['value'].label = field.help_text
+1

, :

LabelField
ValueField

, "LabelField" , "ValueField". , - "LabelField" "ValueField".

"disabled" "LabelField" true:

form.fields['labelfield'].widget.attrs['disabled'] = True

"labelfield", . , , , Django .

+1

Source: https://habr.com/ru/post/1716467/


All Articles