Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django: relationship between BoundField and Form Field?

Tags:

django

As per official documentation:

  • BoundField: A Field plus data (source) (documentation)
  • Form Field: Manages Form data (source) (documentation)

If I look at the source code, they both seem independent classes, each inheriting from the base object. However, they both seem related in some way, it's just that I am not able to figure out how.

The only relationship I could find is that the Field class defines a method get_bound_field. The docstring for this method reads, "Return a BoundField instance that will be used when accessing the form field in a template."

Question: Can you help me figure out the relationship between BoundField and Form Field? And in case if it is, how exactly are they different? Or which one to use when?

like image 655
The Wanderer Avatar asked Feb 26 '17 09:02

The Wanderer


1 Answers

You declare form fields when defining your form. They are what determine the types and names of the fields, what widgets they use, and what validation they run.

You never create bound fields directly; they are produced - via that method - when you instantiate a form and iterate over its fields. They deal with displaying the field and populating it with any values. The BoundField has an attribute .field which contains the form field. The relationship is one of composition, not inheritance.

like image 62
Daniel Roseman Avatar answered Nov 17 '22 15:11

Daniel Roseman