Opened 3 years ago

Closed 3 years ago

#20089 closed New feature (wontfix)

Give reference from widget to its field

Reported by: ram@… Owned by: nobody
Component: Forms Version: master
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


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 Changed 3 years ago by timo

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to needsinfo
  • Status changed from new to closed

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 Changed 3 years ago by coolRR

  • Resolution needsinfo deleted
  • Status changed from closed to new

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 Changed 3 years ago by carljm

  • Resolution set to wontfix
  • Status changed from new to closed

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