Reputation: 283043
Forms
have Fields
, Fields
have a value
. However, they only get a value
after the form has been submitted.
field.value
,
None
prior to posting, and fill it in afterwords? form.data['field']
. FieldWithData
to avoid any inconsistent states (if you have an object of this type, you know it has data) and allows me to set the data in the initializer rather than accessing attributes directly (although I guess this isn't so different from using a setter)Form
object? Options:
form.fields['name'].value
(how it's presently being stored internally)form.data['field']
(create a proxy "data" class that retrieves the real data off the field, or re-arrange the internals to actually store the data like this)form.field.value
- looks fairly nice, but then I'd have two references to the same field, one as form.field
and one as form.fields['field']
which I need internally so that I can iterate over themToo many design decisions. Driving me nuts. This is what sucks about solo'ing a project.
Upvotes: 1
Views: 211
Reputation: 100816
I would keep a form's definition and the form's values from submission separate. I.e. I would not have a value
attribute on the Field(Definition) objects.
To work with submitted values, I would probably use a dict. You could let the Form class handle the creation of this dict:
# assuming my_form is a Form object and request represents the HTTP request
form_values = my_form.values_from_request(request)
print(form_values["Name"])
The values_from_request
method would iterate through the Form's Field(Definition)s to get the submitted data from the HTTP request. The method could also do stuff like validation and data type conversion.
Upvotes: 1
Reputation: 1242
It really depends on how you interact with the structures in question. Do you manipulate Form
and Field
objects prior to assigning them values? Do you need to frequently iterate over all the given Fields
? Do you need Form
once it's been submitted? Etc.
I'd suggest writing some/all of the code that uses Form
and figure out how you want to interact with Form
data, and what your ideal interface would look like.
Upvotes: 1