I'm having a FieldError
.
I have a model:
class Foo(models.Model):
__bar = models.TextField(default=lambda: cPickle.dumps(set()), primary_key=True)
def get_bar(self):
return cPickle.loads(str(self.__bar))
def set_bar(self, values):
self.__bar = cPickle.dumps(values)
bar = property(get_bar, set_bar)
I have registered it with the admin in admin.py:
admin.site.register(Foo)
When I runserver and go to /admin, I see Foo in the list. If I click "Add", it works fine, showing the form to add a new Foo. However, if I click "save and add another" or "change" or "Foos", I get a FieldError
FieldError at /admin/appname/Foo/
Cannot resolve keyword '_Foo' into field. Choices are: _Foo__bar, appname
The traceback is:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/appname/foo/
Django Version: 1.2.4
Python Version: 2.6.6
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'pagination',
'apps.appname']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'pagination.middleware.PaginationMiddleware')
Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
100. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/options.py" in wrapper
265. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in _wrapped_view
76. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
78. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/sites.py" in inner
190. return view(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in _wrapper
21. return decorator(bound_func)(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in _wrapped_view
76. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in bound_func
17. return func(self, *args2, **kwargs2)
File "/usr/local/lib/python2.6/dist-packages/django/con开发者_JAVA百科trib/admin/options.py" in changelist_view
1097. 'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py" in __len__
80. self._result_cache = list(self.iterator())
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py" in iterator
271. for row in compiler.results_iter():
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py" in results_iter
677. for rows in self.execute_sql(MULTI):
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py" in execute_sql
722. sql, params = self.as_sql()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py" in as_sql
57. ordering, ordering_group_by = self.get_ordering()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py" in get_ordering
346. self.query.model._meta, default_order=asc):
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py" in find_ordering_name
375. opts, alias, False)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py" in setup_joins
1215. "Choices are: %s" % (name, ", ".join(names)))
Exception Type: FieldError at /admin/appname/foo/
Exception Value: Cannot resolve keyword '_Foo' into field. Choices are: _Foo__bar, appname
What could be going on here? I did some searching, and I found similar errors but they seem to have something to do with many-to-many fields, of which I have none. There are other models that reference this one as a foreign key, but that seems unlikely to cause the problem.
Because you've prefixed __bar
with two underscores, Python is performing name mangling (doc link) in an attempt to enforce private access to the variable.
Private variables, of course, don't exist in Python, but by using the double-underscore convention you've asked Python to reformat __bar
as _(classname)__bar
, which is why Django is telling you that "Choices are: _Foo__bar
...".
If you wish to signal that bar
shouldn't be used outside out the class, I'd recommend the single-underscore notation (also discussed at the link above), which should solve the field issue you're experiencing as single-underscore prefixes are not name mangled.
精彩评论