Issue with fields.py>Field>get_default() throwing error 'request' while parsing CurrentUserDefault() #8474
Replies: 1 comment
-
Something interesting is that I can get the error to go away and get the desired response without changing anything within the serializers or the view. import warnings
from rest_framework.fields import Field as packageField
from rest_framework.fields import SkipField
from rest_framework import RemovedInDRF313Warning
class empty:
"""
This class is used to represent no data being provided for a given input
or output value.
It is required because `None` may be a valid input or output value.
"""
pass
class Field(packageField):
def run_validation(self, data=empty):
(is_empty_value, data) = self.validate_empty_values(data)
if is_empty_value:
return data
value = self.to_internal_value(data)
self.run_validators(value)
return value
class HiddenField(Field):
def __init__(self, **kwargs):
assert 'default' in kwargs, 'default is a required argument.'
kwargs['write_only'] = True
super().__init__(**kwargs)
def get_value(self, dictionary):
# We always use the default value for `HiddenField`.
# User input is never provided or accepted.
return empty
def to_internal_value(self, data):
return data If you comment out the entirety of Chasing the error further (into validation_empty_values -> get_default), if you copy these functions from DRF into the Field class above then the error returns. e.g. class Field(packageField):
def run_validation(self, data=empty):
(is_empty_value, data) = self.validate_empty_values(data)
if is_empty_value:
return data
value = self.to_internal_value(data)
self.run_validators(value)
return value
def validate_empty_values(self, data):
if self.read_only:
return (True, self.get_default())
if data is empty:
if getattr(self.root, 'partial', False):
raise SkipField()
if self.required:
self.fail('required')
return (True, self.get_default())
if data is None:
if not self.allow_null:
self.fail('null')
# Nullable `source='*'` fields should not be skipped when its named
# field is given a null value. This is because `source='*'` means
# the field is passed the entire object, which is not null.
elif self.source == '*':
return (False, None)
return (True, None)
return (False, data)
def get_default(self):
if self.default is empty or getattr(self.root, 'partial', False):
# No default, or this is a partial update.
raise SkipField()
if callable(self.default):
if hasattr(self.default, 'set_context'):
warnings.warn(
"Method `set_context` on defaults is deprecated and will "
"no longer be called starting with 3.13. Instead set "
"`requires_context = True` on the class, and accept the "
"context as an additional argument.",
RemovedInDRF313Warning, stacklevel=2
)
self.default.set_context(self)
if getattr(self.default, 'requires_context', False):
return self.default(self)
else:
return self.default()
return self.default From this the line that seems to be throwing the error is Sorry I realise that's the opposite of concise but I'm not sure how else to describe the issue and my observations. |
Beta Was this translation helpful? Give feedback.
-
Not sure if I'm doing something wrong or whether this is actually an issue but adding it here for now.
While running serializer.is_valid() I get an error thrown 'request'.
Chasing the error it seems to be occurring within fields.py>Field>get_default() with
return self.default(self)
(with the exact line being 516 in fields.py) while processing the fieldHiddenField(default=serializers.CurrentUserDefault(), write_only=True)
Below is an example view and serializers which reproduce this issue.
I'm not sure if I'm doing something DRF does not support here, the idea was that drf-spectacular requires a serializer to display the desired response so I would reuse that serializer, rather then recreate the desired response manually as shown below.
I'm almost certain all this effort is not worth not adding 'deleted_example_model' twice (within view and serializer).
Despite this I'm still curious where the error is coming from and what 'request' is trying to say, any help/insight would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions