Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError when CRUD event involves AnonymousUser #320

Open
sgordon16 opened this issue Dec 27, 2024 · 0 comments · May be fixed by #321
Open

ValueError when CRUD event involves AnonymousUser #320

sgordon16 opened this issue Dec 27, 2024 · 0 comments · May be fixed by #321

Comments

@sgordon16
Copy link

sgordon16 commented Dec 27, 2024

ValueError when CRUD event involves AnonymousUser: Field 'id' expected a number but got ''

Describe the bug

A ValueError occurs when a CRUD event is performed by an AnonymousUser. Instead of returning None for the user field, an empty string ('') is being passed.

Error Details

*** ValueError: Field 'id' expected a number but got ''.
Traceback (most recent call last):
  File "/app/.venv/lib/python3.11/site-packages/django/db/models/fields/related_descriptors.py", line 218, in __get__
    rel_obj = self.field.get_cached_value(instance)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/django/db/models/fields/mixins.py", line 15, in get_cached_value
    return instance._state.fields_cache[cache_name]
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: 'user'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/app/.venv/lib/python3.11/site-packages/django/db/models/fields/__init__.py", line 2053, in get_prep_value
    return int(value)
           ^^^^^^^^^^
ValueError: invalid literal for int() with base 10: ''

Cause

The user field is a nullable primary key field that expects either an integer or None. When the user is anonymous, the field should be set to None, but it is receiving an empty string instead.

Expected behavior

The user field should return None for an AnonymousUser, not an empty string.

@sgordon16 sgordon16 linked a pull request Dec 27, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant