When creating a form, I wish to use one field on the model as a label to go with another field that I'm updating.
I've overridden the BaseModelFormSet with a new ___init____ method, like this:
class BaseMyFormSet(BaseModelFormSet):
def __init__(self, *args, **kwargs):
super(BaseMyFormSet, self).__init__(*args, **kwargs)
for form in self.forms:
form.fields['value'].label = ???
How do I reference the other field in the model so that I can use it as the label value?
(Alternatively, if there's a better way to override the label in the way I need, that would be very helpful as well.)
Thanks.
I'm not entirely clear on your goal, are you trying to use the value of a field for a particular instance of a model or are you just trying to use the model field's own name or help_text attribute from the model definition?
I'm guessing you want to do the latter. If so, you can access the model information like this in your init method:
for form in self.forms:
opts = self.model._meta
field = opts.get_field("yourfield")
form.fields['value'].label = field.name
Or
form.fields['value'].label = field.help_text
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With