#9903 closed (invalid)
Model._meta._name_map is missing unless you load an object from the database
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Uncategorized | Version: | 1.0 |
Severity: | 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 need to be able to validate that input parameters are the same length as the max_length field. I figured out how to get back the field object use Model._meta.get_field_by_name()
, but I still want to be able to check whether the field exists before I call that method. My code uses Model._meta._name_map
, but it doesn't look like it is instantiated until after you load an object from the database. This seems wrong to me, considering that it is suppose to be an attribute on the model class and not dependent on a particular
In my example below, I show you can't get to _meta._name_map
until after you load an object:
In [1]: from graffiti.models import * In [2]: len(Replica._meta._name_map) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /home/pavlo/Documents/Graffiti/graffiti/src/tracker/<ipython console> in <module>() AttributeError: 'Options' object has no attribute '_name_map' In [3]: replica = Replica() In [4]: len(Replica._meta._name_map) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /home/pavlo/Documents/Graffiti/graffiti/src/tracker/<ipython console> in <module>() AttributeError: 'Options' object has no attribute '_name_map' In [5]: replica = Replica.objects.get(id=1) In [6]: len(Replica._meta._name_map) Out[6]: 19
_name_map
is an internal cache that you shouldn't access directly. Instead, take a look at theReplica._meta.get_field
method.