Functionality method hidden_fields()


I’m trying to find a bug in an application and now I’m having a problem understanding the hidden_fields method of the BaseForm class.

I have now looked at several calls, but can’t figure out the function. self has no attribute with the name field in any call, the only attribute with a similar name is fields.

Where is the field attribute obtained from then?



There’s a lot of metaprogramming involved in both Forms and Models, it can be a bit confusing at first.

hidden_fields, as you’ve linked to it is:
return [field for field in self if field.is_hidden]

This means it’s iterating over the current form instance, and for each iteration, it’s going to assign whatever value returned by the iterator to the name field, checking the is_hidden attribute of the element returned, and building a list from those fields for which that condition is True.

This leads you to the __iter__ method on the class, which looks like this:

    def __iter__(self):
        for name in self.fields:
            yield self[name]

So no, there’s no attribute directly on the class named field, only fields containing the set of fields in that form.

Thank you for the explanation.