Opened 11 years ago

Closed 11 years ago

#20089 closed New feature (wontfix)

Give reference from widget to its field

Reported by: ram@… Owned by: nobody
Component: Forms Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I needed to do some customization of Django's forms, and I needed a way to get the field belonging to a widget. It would have helped if Widget had a .field attribute.

Change History (3)

comment:1 by Tim Graham, 11 years ago

Resolution: needsinfo
Status: newclosed

I'm not sure what you're looking to do can't be done with the existing design. How do you have access to the widget but not the field?

comment:2 by Ram Rachum, 11 years ago

Resolution: needsinfo
Status: closednew

I was monkeypatching a method on the Widget, and the function I was adding needed to access the field.

I know monkeypatching is considered bad, but I think we shouldn't make people's lives hard when they try to do it.

comment:3 by Carl Meyer, 11 years ago

Resolution: wontfix
Status: newclosed

It's not monkeypatching that's bad so much in this case, it's turning a unidirectional dependency (a field needs a widget) into a bidirectional dependency (a field needs a widget and a widget needs a field). Bidirectional dependencies are a sign of overly-coupled code that's violating separation of layers, and it makes the code harder to test and maintain. If every widget knew about "its field" (a concept that currently does not exist; widgets can exist independently), now you can't even instantiate a widget for independent testing without providing a field too.

Instead of thinking "this widget needs to know about its field", consider what specific piece of information your widget actually needs, and pass that information in in its constructor. If that's not possible, then it's likely you are writing code at the widget level that belongs at the field level instead. Hard to be more specific without seeing the actual code you were trying to write.

Note: See TracTickets for help on using tickets.
Back to Top