#22510: Form fields cannot have names that are present as attributes on the form ---------------------------------+-------------------------------------- Reporter: gc@… | Owner: nobody Type: Bug | Status: new Component: Forms | Version: 1.7-beta-2 Severity: Release blocker | Resolution: Keywords: | Triage Stage: Unreviewed Has patch: 1 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 ---------------------------------+--------------------------------------
Comment (by gcbirzan): Replying to [comment:2 loic84]: > At the time I suggested we only allowed shadowing with `None`, @mjtamlyn convinced me that we should follow normal Python inheritance/shadowing and I think that was a good decision. True, but the problem is that the builtin Form fields also have this property. It's made worse by, for example, media being set by magic via the previous meta class, so it's set on each of the classes that inherit from Form. > I see value in matching as closely as possible the MRO behavior, other than the fact that `Field` magically remove themselves from the class (whether shadowed or not), we are pretty good on that front. > > Inheritance makes this kind of clash possible, but a single form couldn't define both a `Field` and an attribute anyway. > > To expose a `media=42` attribute like in the test of the proposed patch, one can set `self.media` in `__init__()`, or ensure that it appears first in the MRO. By ensuring it appears first in the MRO it won't shadow any field, and it will not be shadowed itself because fields are removed from the class. To be honest, I don't remember why media = 42 is in there, it doesn't really matter, it happens either way. As for the proposed solution, with `__init__()` is ugly and, at the very least, needs documentation explaining that things you don't manually set might have this effect. > We document: > > {{{ > It's possible to opt-out from a ``Field`` inherited from a parent class by > shadowing it. While any non-``Field`` value works for this purpose, it's > recommended to use ``None`` to make it explicit that a field is being > nullified. > }}} > > Which is exactly the case described in the test of the proposed patch. True. But even so, there's another bug that's fixed by my patch. Reordering the shadowing and collecting means that, if nothing else, defining media as a field on a form will work, if there's no other inheritance. -- Ticket URL: <https://code.djangoproject.com/ticket/22510#comment:3> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/071.65f2e3a823f8f38ba5e9643f646ccd9c%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.