每当我决定在list_display中列出一个布尔字段时,例如'active'(参见示例)
class CompanyAdmin(admin.ModelAdmin):
fields = ['name', 'created']
list_display = ['name', 'active', 'created']
我收到以下错误...
KeyError at /admin/web/company/
'\x01'
这会在我尝试列出的任何布尔值上发生。我正在使用grappelli作为管理皮肤。我已经包含了下面的完整错误和我的模型。
型号:
class Company(models.Model):
companyid = models.CharField(max_length=255, primary_key=True, db_column='companyID')
name = models.CharField(max_length=105)
active = models.BooleanField(default=False)
created = models.DateTimeField()
modified = models.DateTimeField(null=True, blank=True)
class Meta:
db_table = u'company'
错误: 环境:
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/web/company/
Django Version: 1.4.2
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.flatpages',
'django.contrib.messages',
'django.contrib.staticfiles',
'grappelli.dashboard',
'grappelli',
'django.contrib.admin',
'django.contrib.admindocs',
'web')
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')
Template error:
In template /Users/user/Documents/workspace/Ares/grappelli/templates/admin/change_list.html, error at line 222
212 : {% blocktrans count cl.formset.errors|length as counter %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktrans %}
213 : </p>
214 : {{ cl.formset.non_form_errors }}
215 : {% endif %}
216 : <!-- MANAGEMENT FORM -->
217 : {% if cl.formset %}
218 : {{ cl.formset.management_form }}
219 : {% endif %}
220 : <!-- CHANGELIST-RESULTS -->
221 : {% block result_list %}
222 : {% result_list cl %}
223 : {% endblock %}
224 : </section>
225 : <!-- PAGINATION BOTTOM -->
226 : {% if not cl.result_count == 0 %}
227 : {% block pagination_bottom %}
228 : <div class="grp-module">
229 : <div class="grp-row">{% pagination cl %}</div>
230 : </div>
231 : {% endblock %}
232 : {% endif %}
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
136. response = response.render()
File "/Library/Python/2.7/site-packages/django/template/response.py" in render
104. self._set_content(self.rendered_content)
File "/Library/Python/2.7/site-packages/django/template/response.py" in rendered_content
81. content = template.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
140. return self._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
823. bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
123. return compiled_parent._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
823. bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
123. return compiled_parent._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
823. bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
62. result = block.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
823. bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
62. result = block.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
823. bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
1178. _dict = func(*resolved_args, **resolved_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in result_list
278. 'results': list(results(cl))}
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in results
256. yield ResultList(None, items_for_result(cl, res, None))
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in __init__
248. super(ResultList, self).__init__(*items)
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in items_for_result
204. result_repr = display_for_field(value, f)
File "/Library/Python/2.7/site-packages/django/contrib/admin/util.py" in display_for_field
321. return _boolean_icon(value)
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in _boolean_icon
164. {True: 'yes', False: 'no', None: 'unknown'}[field_val])
Exception Type: KeyError at /admin/web/company/
Exception Value: '\x01'
答案 0 :(得分:4)
我在一个拥有Hibernate模型的现有MySQL数据库上使用一些Django模型遇到了类似的问题。它使用BIT(1)作为布尔类型。只要您的1和0值在Python中显示为\x01
和\x00
,您就应该能够挽救下面的BooleanField
以下是一些线索:
class MyBooleanField(models.BooleanField):
__metaclass__ = models.SubfieldBase
def get_internal_type(self):
return "MyBooleanField"
def db_type(self):
return 'bit(1)'
def to_python(self, value):
if value in (True, False): return value
if value in ('t', 'True', '1', '\x01'): return True
if value in ('f', 'False', '0', '\x00'): return False
def get_db_prep_value(self, value):
return 0x01 if value else 0x00
来自:http://djangosnippets.org/snippets/1869/
然后在你的模特中:
class Company(models.Model):
active = models.MyBooleanField(default=False)