我在Django中将DateField设为default=timezone.now
你好,我是Densyakun。
我猜是因为重新开始开发一段时间没有处理的应用程序,所以用Django在DateField中写了default=timezone.now才导致这个问题。
你想要做什么?
-
- 日付のみを扱いたい
-
- 値を無しにできるようにしたい
- 入力しやすくするため、初期値を今日にしたい (auto_系は強制なので目的と違う)
环境
-
- Windows 10 Pro
-
- Python 3.7.7
-
- pip 20.2.1
- Django 3.0.7
千万不要写的代码.
from django.db import models
from django.utils import timezone
class Production(models.Model):
first_date = models.DateField(default=timezone.now)
错误
由于Django和sqlite3的内部错误,我认为这是一个错误。
Traceback (most recent call last):
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 156, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 154, in _get_response
response = response.render()
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\response.py", line 106, in render
self.content = self.rendered_content
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\response.py", line 83, in rendered_content
content = template.render(context, self._request)
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 171, in render
return self._render(context)
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\defaulttags.py", line 308, in render
if match:
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 272, in __bool__
self._fetch_all()
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 1186, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\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 "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 1065, in execute_sql
cursor.execute(sql, params)
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\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 "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\sqlite3\base.py", line 298, in execute
return Database.Cursor.execute(self, query, params)
File "C:\Users\densy\AppData\Local\Programs\Python\Python37\lib\sqlite3\dbapi2.py", line 64, in convert_date
return datetime.date(*map(int, val.split(b"-")))
ValueError: invalid literal for int() with base 10: b'26 18:02:28.712485'
我想要解决
使用数据的模板页面和admin页面无法访问,也无法在shell中使用Production.objects.all()(出现相同错误)。Production.objects之前没有问题。
DateField只支持日期,但在Shell中查看时却变成了datetime。
首先,将models.DateTimeField()更改为一度,然后进行makemigrations和migrate操作,
在确认后,错误消失了,并且有显示时间。
然后,我使用Django的shell手动修正了数据,然后将模型更改为DateField,并将默认值设为datetime.date.today,但这是无意义的,因为日期没有时区。
from django.db import models
from datetime import date
class Production(models.Model):
first_date = models.DateField(default=date.today)
我执行了makemigrations和migrate,但问题没有解决。
使用十进制int()函数时出现无效的文字,以基数10: b’25 15:00:00’。
这个错误出现了,并且时间已经改变了,但是错误没有改变。
此外,由于我希望在我的情况下不使用值,因此我将其设置为blank=True和null=True。
我原本以为这可能是原因,但是后来发现如果只有blank=True或者只有null=True,会出现另一个错误,所以我就保持了原样。
更新Python、pip、Django
我将Python版本更改为3.8.5并尝试了一下,但是仍然出现了相同的错误。
然后,我将pip更新到了20.2.2版本,将Django更新到了3.1版本。
最终结果
为了避免重新创建数据,最终我选择了使用DateTimeField并将其设置为timezone.now。
尽管时间被添加了,但我们会在显示端进行处理。