學習Django內置的基於類的通用視圖出現的錯誤:OverflowError: Python int too large to convert to C long
項目環境:win10 + python 3.7 + django 2.1
books.models部分代碼:
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField('Author')
publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE)
publication_date = models.DateField()
books.views部分代碼:
class PublisherDetail(DetailView):
model = Publisher
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['book_list'] = Book.objects.all()
return context
接下來啓動項目,訪問時出現如下情況:
Traceback (most recent call last):
File "D:\python\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "D:\python\lib\site-packages\django\core\handlers\base.py", line 156, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\python\lib\site-packages\django\core\handlers\base.py", line 154, in _get_response
response = response.render()
File "D:\python\lib\site-packages\django\template\response.py", line 106, in render
self.content = self.rendered_content
File "D:\python\lib\site-packages\django\template\response.py", line 83, in rendered_content
content = template.render(context, self._request)
File "D:\python\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "D:\python\lib\site-packages\django\template\base.py", line 171, in render
return self._render(context)
File "D:\python\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "D:\python\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "D:\python\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "D:\python\lib\site-packages\django\template\defaulttags.py", line 166, in render
len_values = len(values)
File "D:\python\lib\site-packages\django\db\models\query.py", line 250, in __len__
self._fetch_all()
File "D:\python\lib\site-packages\django\db\models\query.py", line 1186, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "D:\python\lib\site-packages\django\db\models\query.py", line 54, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "D:\python\lib\site-packages\django\db\models\sql\compiler.py", line 1065, in execute_sql
cursor.execute(sql, params)
File "D:\python\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "D:\python\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "D:\python\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "D:\python\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "D:\python\lib\site-packages\django\db\backends\sqlite3\base.py", line 298, in execute
return Database.Cursor.execute(self, query, params)
File "D:\python\lib\sqlite3\dbapi2.py", line 64, in convert_date
return datetime.date(*map(int, val.split(b"-")))
OverflowError: Python int too large to convert to C long
[21/Aug/2019 19:15:32] "GET /books/publishers/1 HTTP/1.1" 500 140379
折騰了好久都沒找到原因,然後就試着改了model中Book類中的
publication_date = models.DateField()
改爲:
publication_date = models.DateTimeField(auto_now_add=True)
然後將數據庫中的信息更新一下,重新訪問就可以了。