Django error: 'unicode' object has no attribute 'pop'
Django error: 'unicode' object has no attribute 'pop'
我在 Django 的管理中同时使用了 EmbeddedModelFields 和 ListFields。为此,我使用以下教程:
https://gist.github.com/jonashaag/1200165
https://gist.github.com/ielshareef/3011156
我的 models.py 包含以下内容,
class EmbedOverrideField(EmbeddedModelField):
def formfield(self, **kwargs):
return models.Field.formfield(self, ObjectListField, **kwargs)
class CategoryField(ListField):
def formfield(self, **kwargs):
return models.Field.formfield(self, StringListField, **kwargs)
class Post(models.Model):
pub_date = models.DateTimeField(auto_now_add=True, null=True) #
title = models.CharField(max_length=255)
post_text = models.TextField()
tags = CategoryField()
comments = CategoryField(EmbedOverrideField('Comment'))
def __str__(self):
return self.post_text
在 admin.py 我有,
site.register(Post)
在管理站点上,我尝试单击 "Post" 以查看其中的内容,但出现上述错误。为什么会这样?
谢谢!
编辑:我不知道这是否有任何帮助,但是当我尝试添加新的 Post 时,我看到一个表格,我可以在其中输入 post 标题、文本、标签和评论。当我实际保存它时,我得到了错误,
Expected instance of type <class 'polls.models.Comment'>, not <type 'unicode'>.
需要说明的是,我确实在使用 MongoDB 作为我的后端。
以下是完整的异常堆栈跟踪:
File "/Users/Anupa/myproject/lib/python2.7/site-
packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Anupa/myproject/lib/python2.7/site- packages/django/contrib/admin/options.py" in wrapper
466. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
198. return view(request, *args, **kwargs)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
25. return func(self, *args2, **kwargs2)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/contrib/admin/options.py" in changelist_view
1445. 'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/db/models/query.py" in __len__
77. self._fetch_all()
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
857. self._result_cache = list(self.iterator())
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/db/models/query.py" in iterator
220. for row in compiler.results_iter():
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/basecompiler.py" in results_iter
381. yield self._make_result(entity, fields)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/basecompiler.py" in _make_result
431. value = self.ops.value_from_db(value, field)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/base.py" in value_from_db
254. return self._value_from_db(value, *self._convert_as(field))
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django_mongodb_engine/base.py" in _value_from_db
154. value, field, field_kind, db_type)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/base.py" in _value_from_db
380. field_kind, db_type)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/base.py" in _value_from_db_collection
513. return list(value)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/base.py" in <genexpr>
508. for subvalue in value)
File "/Users/Anupa/myproject/lib/python2.7/site- packages/django_mongodb_engine/base.py" in _value_from_db
154. value, field, field_kind, db_type)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/base.py" in _value_from_db
385. field_kind, db_type)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/base.py" in _value_from_db_model
587. embedded_model = field.stored_model(value)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/fields.py" in stored_model
299. module = column_values.pop('_module', None)
Exception Type: AttributeError at /admin/polls/post/
Exception Value: 'unicode' object has no attribute 'pop'
你忘记评论模型了吗?
编辑:
问题是有一个 Post 实体将评论作为字符串而不是 Comment 实例。
我能够通过调用重现此错误,
from polls.models import Post
Post.objects.get()
我通过切换到一个全新的数据库来修复它。不过,我在发表评论时的错误仍然存在。 :-( 知道如何解决这个问题吗?
我在 Django 的管理中同时使用了 EmbeddedModelFields 和 ListFields。为此,我使用以下教程: https://gist.github.com/jonashaag/1200165 https://gist.github.com/ielshareef/3011156
我的 models.py 包含以下内容,
class EmbedOverrideField(EmbeddedModelField):
def formfield(self, **kwargs):
return models.Field.formfield(self, ObjectListField, **kwargs)
class CategoryField(ListField):
def formfield(self, **kwargs):
return models.Field.formfield(self, StringListField, **kwargs)
class Post(models.Model):
pub_date = models.DateTimeField(auto_now_add=True, null=True) #
title = models.CharField(max_length=255)
post_text = models.TextField()
tags = CategoryField()
comments = CategoryField(EmbedOverrideField('Comment'))
def __str__(self):
return self.post_text
在 admin.py 我有,
site.register(Post)
在管理站点上,我尝试单击 "Post" 以查看其中的内容,但出现上述错误。为什么会这样?
谢谢!
编辑:我不知道这是否有任何帮助,但是当我尝试添加新的 Post 时,我看到一个表格,我可以在其中输入 post 标题、文本、标签和评论。当我实际保存它时,我得到了错误,
Expected instance of type <class 'polls.models.Comment'>, not <type 'unicode'>.
需要说明的是,我确实在使用 MongoDB 作为我的后端。
以下是完整的异常堆栈跟踪:
File "/Users/Anupa/myproject/lib/python2.7/site-
packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Anupa/myproject/lib/python2.7/site- packages/django/contrib/admin/options.py" in wrapper
466. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
198. return view(request, *args, **kwargs)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
25. return func(self, *args2, **kwargs2)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/contrib/admin/options.py" in changelist_view
1445. 'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/db/models/query.py" in __len__
77. self._fetch_all()
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
857. self._result_cache = list(self.iterator())
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/db/models/query.py" in iterator
220. for row in compiler.results_iter():
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/basecompiler.py" in results_iter
381. yield self._make_result(entity, fields)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/basecompiler.py" in _make_result
431. value = self.ops.value_from_db(value, field)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/base.py" in value_from_db
254. return self._value_from_db(value, *self._convert_as(field))
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django_mongodb_engine/base.py" in _value_from_db
154. value, field, field_kind, db_type)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/base.py" in _value_from_db
380. field_kind, db_type)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/base.py" in _value_from_db_collection
513. return list(value)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/base.py" in <genexpr>
508. for subvalue in value)
File "/Users/Anupa/myproject/lib/python2.7/site- packages/django_mongodb_engine/base.py" in _value_from_db
154. value, field, field_kind, db_type)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/base.py" in _value_from_db
385. field_kind, db_type)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/db/base.py" in _value_from_db_model
587. embedded_model = field.stored_model(value)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/djangotoolbox/fields.py" in stored_model
299. module = column_values.pop('_module', None)
Exception Type: AttributeError at /admin/polls/post/
Exception Value: 'unicode' object has no attribute 'pop'
你忘记评论模型了吗?
编辑:
问题是有一个 Post 实体将评论作为字符串而不是 Comment 实例。
我能够通过调用重现此错误,
from polls.models import Post
Post.objects.get()
我通过切换到一个全新的数据库来修复它。不过,我在发表评论时的错误仍然存在。 :-( 知道如何解决这个问题吗?