Django 管理站点。 'Delete multiple objects' 产生 ValidationError
Django administration Site. 'Delete multiple objects' produces ValidationError
尝试删除与其他两个表具有一对一关系的多行时,我收到以下错误:
['“March 21, 2022” value has an invalid date format. It must be in YYYY-MM-DD format.']
模型设置如下:
class Media(models.Model):
date = models.DateField(primary_key=True, unique=True)
received_url = models.CharField(max_length=200, blank=True, null=True)
class Api(models.Model):
media = models.OneToOneField(Media, on_delete=models.CASCADE)
request_url = models.CharField(max_length=200)
class Text(models.Model):
media = models.OneToOneField(Media, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
copyright = models.CharField(max_length=200, blank=True, null=True)
我正在尝试删除 主页 › My_App_Main › 媒体 › 从 Django 管理站点[=68] 删除多个对象 =].这向我展示了以下内容:
你确定吗?
您确定要删除选定的媒体吗?以下所有对象及其相关项目都将被删除:
总结
Medias: 2
Apis: 2
Texts: 2
对象
Media: 2022-03-21
Api: 2022-03-21
Text: 2022-03-21
Media: 2022-03-20
Api: 2022-03-20
Text: 2022-03-20
然后我点击是的,我确定
先触发再触发错误。
检查浏览器网络日志中的 POST 请求,我注意到日期格式似乎有误:
_selected_action […]
0 "March+21,+2022"
1 "March+20,+2022"
action "delete_selected"
post "yes"
我在 Firefox 和 Chrome 中都试过了。
检查来自 Django 的数据 shell 没有发现不正确的地方。数据按预期返回:
>>> for field in Media.objects.all():
... print(f'{type(field.date)}({field.date})')
...
<class 'datetime.date'>(2022-03-21)
<class 'datetime.date'>(2022-03-20)
>>> for field in Text.objects.all():
... print(f'{type(field.media.date)}({field.media.date})')
...
<class 'datetime.date'>(2022-03-21)
<class 'datetime.date'>(2022-03-20)
>>> for field in Api.objects.all():
... print(f'{type(field.media.date)}({field.media.date})')
...
<class 'datetime.date'>(2022-03-21)
<class 'datetime.date'>(2022-03-20)
在我看来,这可能与 Django 管理站点对日期格式做了一些奇怪的事情有关?或者可能是浏览器默认设置问题?或者甚至是我需要在 Django 设置文件中设置的内容?如果是这种情况,人们将如何改变这种行为?
在浏览器中返回的错误:
/admin/my_app_main/media/
处的验证错误
['“March 21, 2022” value has an invalid date format. It must be in YYYY-MM-DD format.']
请求方法:POST
请求 URL:http://127.0.0.1:11111/admin/my_app_main/media/
姜戈版本:4.0.3
异常类型:ValidationError
异常值:
['“March 21, 2022” value has an invalid date format. It must be in YYYY-MM-DD format.']
异常位置:/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/fields/init.py, line 1370, in to_python
Python 可执行文件:/home/user/.pyenv/versions/my_app/bin/python
Python版本:3.8.2
Python路径:
['/home/user/git/user/my_app',
'/home/user/git/user/my_app',
'/home/user/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/_vendored/pydevd',
'/home/user/.pyenv/versions/3.8.2/lib/python38.zip',
'/home/user/.pyenv/versions/3.8.2/lib/python3.8',
'/home/user/.pyenv/versions/3.8.2/lib/python3.8/lib-dynload',
'/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages',
'/home/user/git/user/my_app/apps']
服务器时间:2022 年 3 月 21 日,星期一 14:40:59 +0000
回溯:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:11111/admin/my_app_main/media/
Django Version: 4.0.3
Python Version: 3.8.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'apps.my_app_main.apps.MyAppMainConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/contrib/admin/options.py", line 683, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/views/decorators/cache.py", line 62, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/contrib/admin/sites.py", line 242, in inner
return view(request, *args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/utils/decorators.py", line 46, in _wrapper
return bound_method(*args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/contrib/admin/options.py", line 1983, in changelist_view
response = self.response_action(
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/contrib/admin/options.py", line 1586, in response_action
queryset = queryset.filter(pk__in=selected)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/query.py", line 1071, in filter
return self._filter_or_exclude(False, args, kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/query.py", line 1089, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/query.py", line 1096, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1466, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1496, in _add_q
child_clause, needed_inner = self.build_filter(
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1412, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1242, in build_lookup
lookup = lookup_class(lhs, rhs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/lookups.py", line 27, in __init__
self.rhs = self.get_prep_lookup()
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/lookups.py", line 275, in get_prep_lookup
rhs_value = self.lhs.output_field.get_prep_value(rhs_value)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1404, in get_prep_value
return self.to_python(value)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1370, in to_python
raise exceptions.ValidationError(
Exception Type: ValidationError at /admin/my_app_main/media/
Exception Value: ['“March 21, 2022” value has an invalid date format. It must be in YYYY-MM-DD format.']
我建议不要将 DateField 用作主键。或者有什么理由这样做吗?如果您想检查没有两个实例具有相同的日期,您仍然可以保留 unique=True
。
class Media(models.Model):
date = models.DateField(unique=True)
received_url = models.CharField(max_length=200, blank=True, null=True)
尝试删除与其他两个表具有一对一关系的多行时,我收到以下错误:
['“March 21, 2022” value has an invalid date format. It must be in YYYY-MM-DD format.']
模型设置如下:
class Media(models.Model):
date = models.DateField(primary_key=True, unique=True)
received_url = models.CharField(max_length=200, blank=True, null=True)
class Api(models.Model):
media = models.OneToOneField(Media, on_delete=models.CASCADE)
request_url = models.CharField(max_length=200)
class Text(models.Model):
media = models.OneToOneField(Media, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
copyright = models.CharField(max_length=200, blank=True, null=True)
我正在尝试删除 主页 › My_App_Main › 媒体 › 从 Django 管理站点[=68] 删除多个对象 =].这向我展示了以下内容:
你确定吗?
您确定要删除选定的媒体吗?以下所有对象及其相关项目都将被删除:
总结
Medias: 2
Apis: 2
Texts: 2
对象
Media: 2022-03-21
Api: 2022-03-21
Text: 2022-03-21
Media: 2022-03-20
Api: 2022-03-20
Text: 2022-03-20
然后我点击是的,我确定
先触发再触发错误。
检查浏览器网络日志中的 POST 请求,我注意到日期格式似乎有误:
_selected_action […]
0 "March+21,+2022"
1 "March+20,+2022"
action "delete_selected"
post "yes"
我在 Firefox 和 Chrome 中都试过了。
检查来自 Django 的数据 shell 没有发现不正确的地方。数据按预期返回:
>>> for field in Media.objects.all():
... print(f'{type(field.date)}({field.date})')
...
<class 'datetime.date'>(2022-03-21)
<class 'datetime.date'>(2022-03-20)
>>> for field in Text.objects.all():
... print(f'{type(field.media.date)}({field.media.date})')
...
<class 'datetime.date'>(2022-03-21)
<class 'datetime.date'>(2022-03-20)
>>> for field in Api.objects.all():
... print(f'{type(field.media.date)}({field.media.date})')
...
<class 'datetime.date'>(2022-03-21)
<class 'datetime.date'>(2022-03-20)
在我看来,这可能与 Django 管理站点对日期格式做了一些奇怪的事情有关?或者可能是浏览器默认设置问题?或者甚至是我需要在 Django 设置文件中设置的内容?如果是这种情况,人们将如何改变这种行为?
在浏览器中返回的错误:
/admin/my_app_main/media/
处的验证错误['“March 21, 2022” value has an invalid date format. It must be in YYYY-MM-DD format.']
请求方法:POST 请求 URL:http://127.0.0.1:11111/admin/my_app_main/media/ 姜戈版本:4.0.3 异常类型:ValidationError 异常值:
['“March 21, 2022” value has an invalid date format. It must be in YYYY-MM-DD format.']
异常位置:/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/fields/init.py, line 1370, in to_python Python 可执行文件:/home/user/.pyenv/versions/my_app/bin/python Python版本:3.8.2 Python路径:
['/home/user/git/user/my_app',
'/home/user/git/user/my_app',
'/home/user/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/_vendored/pydevd',
'/home/user/.pyenv/versions/3.8.2/lib/python38.zip',
'/home/user/.pyenv/versions/3.8.2/lib/python3.8',
'/home/user/.pyenv/versions/3.8.2/lib/python3.8/lib-dynload',
'/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages',
'/home/user/git/user/my_app/apps']
服务器时间:2022 年 3 月 21 日,星期一 14:40:59 +0000
回溯:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:11111/admin/my_app_main/media/
Django Version: 4.0.3
Python Version: 3.8.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'apps.my_app_main.apps.MyAppMainConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/contrib/admin/options.py", line 683, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/views/decorators/cache.py", line 62, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/contrib/admin/sites.py", line 242, in inner
return view(request, *args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/utils/decorators.py", line 46, in _wrapper
return bound_method(*args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/contrib/admin/options.py", line 1983, in changelist_view
response = self.response_action(
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/contrib/admin/options.py", line 1586, in response_action
queryset = queryset.filter(pk__in=selected)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/query.py", line 1071, in filter
return self._filter_or_exclude(False, args, kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/query.py", line 1089, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/query.py", line 1096, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1466, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1496, in _add_q
child_clause, needed_inner = self.build_filter(
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1412, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1242, in build_lookup
lookup = lookup_class(lhs, rhs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/lookups.py", line 27, in __init__
self.rhs = self.get_prep_lookup()
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/lookups.py", line 275, in get_prep_lookup
rhs_value = self.lhs.output_field.get_prep_value(rhs_value)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1404, in get_prep_value
return self.to_python(value)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1370, in to_python
raise exceptions.ValidationError(
Exception Type: ValidationError at /admin/my_app_main/media/
Exception Value: ['“March 21, 2022” value has an invalid date format. It must be in YYYY-MM-DD format.']
我建议不要将 DateField 用作主键。或者有什么理由这样做吗?如果您想检查没有两个实例具有相同的日期,您仍然可以保留 unique=True
。
class Media(models.Model):
date = models.DateField(unique=True)
received_url = models.CharField(max_length=200, blank=True, null=True)