首页 > 解决方案 > 部署到 Heroku 后尝试在 Django 中添加类别时获取“'bool' 类型的对象没有 len()”

问题描述

我最近将我的 Django 应用程序部署到 Heroku,在尝试从模型类别的管理中添加类别后,我收到类型错误“'bool' 类型的对象没有 len()”

就像我说的,这是在部署到 Heroku 之后。在本地托管时,一切正常,没有错误。我尝试重置 Postgres 数据库并再次运行迁移,但仍然得到相同的错误。

这是我的模型:

# models.py

from django.db import models
from django.utils.translation import ugettext_lazy as _

class Category(models.Model):
    name = models.CharField(max_length=200, blank=True)
    slug = models.SlugField(blank=True)


    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "categories"
        verbose_name_plural = "categories"

意见:

# views.py

from django.shortcuts import render

from django.shortcuts import render
from django.views.generic import (
    ListView,
    DetailView
)

from .models import Category

class CategoryListView(DetailView):
    template_name = "categories/category_list.html"
    model = Category

这是回溯:

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper
  606.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner
  223.             return view(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in add_view
  1634.         return self.changeform_view(request, None, form_url, extra_context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
  45.         return bound_method(*args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in changeform_view
  1522.             return self._changeform_view(request, object_id, form_url, extra_context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in _changeform_view
  1560.             if all_valid(formsets) and form_validated:

File "/app/.heroku/python/lib/python3.6/site-packages/django/forms/formsets.py" in all_valid
  448.         valid &= formset.is_valid()

File "/app/.heroku/python/lib/python3.6/site-packages/django/forms/formsets.py" in is_valid
  301.         self.errors

File "/app/.heroku/python/lib/python3.6/site-packages/django/forms/formsets.py" in errors
  281.             self.full_clean()

File "/app/.heroku/python/lib/python3.6/site-packages/django/forms/formsets.py" in full_clean
  325.             if not form.has_changed() and i >= self.initial_form_count():

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in has_changed
  2111.                 return super().has_changed()

File "/app/.heroku/python/lib/python3.6/site-packages/django/forms/forms.py" in has_changed
  434.         return bool(self.changed_data)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/functional.py" in __get__
  80.         res = instance.__dict__[self.name] = self.func(instance)

File "/app/.heroku/python/lib/python3.6/site-packages/django/forms/forms.py" in changed_data
  456.             if field.has_changed(initial_value, data_value):

File "/app/.heroku/python/lib/python3.6/site-packages/django/forms/models.py" in has_changed
  1349.         if len(initial) != len(data):

Exception Type: TypeError at /admin/categories/category/add/
Exception Value: object of type 'bool' has no len()

标签: pythondjangopython-3.xheroku

解决方案


推荐阅读