Code

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#9903 closed (invalid)

Model._meta._name_map is missing unless you load an object from the database

Reported by: pavlo@… 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: UI/UX:

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

Attachments (0)

Change History (2)

comment:1 Changed 5 years ago by anonymous

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

_name_map is an internal cache that you shouldn't access directly. Instead, take a look at the Replica._meta.get_field method.

comment:2 Changed 5 years ago by gwilson

last comment was me.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.