Skip to content
Advertisement

Django administration Site. ‘Delete multiple objects’ produces ValidationError

When attempting to delete multiple rows, which have a one to one relationship with two other tables I received the following error:

['“March 21, 2022” value has an invalid date format. It must be in YYYY-MM-DD format.']

The models are set up as such:

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)

I am trying to delete the items in the Home › My_App_Main › Medias › Delete multiple objects from the Django administration site. Which presents me with the following:

Are you sure?

Are you sure you want to delete the selected medias? All of the following objects and their related items will be deleted:

Summary

Medias: 2
Apis: 2
Texts: 2

Objects

Media: 2022-03-21
    Api: 2022-03-21
    Text: 2022-03-21
Media: 2022-03-20
    Api: 2022-03-20
    Text: 2022-03-20

I then click Yes, I’m Sure

Which triggers then triggers the error.

Checking the POST request in the network log for the browser I noted the dates appear to be in the wrong format:

_selected_action    […]
0   "March+21,+2022"
1   "March+20,+2022"
action  "delete_selected"
post    "yes"

I tried in both Firefox and Chrome.

Checking the data from the Django shell nothing stands out as incorrect. The data comes back as expected:

>>> 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)

It seems to me it could something to do with the Django administration site doing something odd with the date format? Or perhaps it’s the browser defaults that are the issue? Or maybe even something I need to set in the Django settings file? If that’s the case how would one go about changing that behaviour?

The error as it comes back in the browser:

ValidationError at /admin/my_app_main/media/

['“March 21, 2022” value has an invalid date format. It must be in YYYY-MM-DD format.']

Request Method: POST Request URL: http://127.0.0.1:11111/admin/my_app_main/media/ Django Version: 4.0.3 Exception Type: ValidationError Exception Value:

['“March 21, 2022” value has an invalid date format. It must be in YYYY-MM-DD format.']

Exception Location: /home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/fields/init.py, line 1370, in to_python Python Executable: /home/user/.pyenv/versions/my_app/bin/python Python Version: 3.8.2 Python Path:

['/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']

Server time: Mon, 21 Mar 2022 14:40:59 +0000

Traceback:

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.']

Advertisement

Answer

I would recommend not using DateField as a primary key. Or is there any reason to do that? You can keep unique=True anyway if you want to check there are no two instances with the same date.

class Media(models.Model):
    date = models.DateField(unique=True)
    received_url = models.CharField(max_length=200, blank=True, null=True)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement