#30543: admin.E108 is raised on fields accessible only via instance.
------------------------------------------------+------------------------
Reporter: ajcsimons | Owner: nobody
Type: Bug | Status: new
Component: Core (System checks) | Version: master
Severity: Normal | Keywords:
Triage Stage: Accepted | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
#28490 caused a regression in 47016adbf54b54143d4cf052eeb29fc72d27e6b1,
i.e.
1. if hasattr(obj.model, item) returns false then we go straight to the
else clause which returns the error,
2. whereas before the else clause did another check for
model._meta.get_field(item) and would only return the error if that raised
a FieldDoesNotExist exception
3. So how is it that hasattr(model, item) can return false, but
model._meta.get_field(item) will return something meaning the check should
not return an error?
4. Answer: the field is a special one which is only valid to access on
instances of the model (whose ModelAdmin we are verifying) and not the
model class itself. An example of this is the PositionField from the
django-positions library (which inherits from djangos
models.IntegerField):
{{{
def __get__(self, instance, owner):
if instance is None:
raise AttributeError("%s must be accessed via instance." %
self.name)
current, updated = getattr(instance, self.get_cache_name())
return current if updated is None else updated
}}}
5. So the simplification of the hasattr branch was valid, but the removal
of the else branch to no longer check get_field doesn't throw before
returning an error was not a refactor but a functionality altering change
which makes this method return errors in cases where it used not to.
--
Ticket URL: <https://code.djangoproject.com/ticket/30543>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.4d8bdef7eefa247c816e2c57b6253074%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.