I’m upgrading a system from Python2.7+Django1.11 to Python3.7.9+Django3.1.4.
This logic path worked OK on the older versions; but on the newer version it triggers an error saying AttributeError: 'list' object has no attribute 'strip'
.
Update:
The direct trigger was Null value
in result = parse_datetime(value.strip())
, and culprit might be in my own source code due to behavior change after upgrade.
I’ve narrowed down to a single line in source code below:
After upgrade, option_2 = self.instance.product_template
started returning wrong result. However, if set a break point in debug mode to briefly pause at the statement, the result will be correct again.
I suspect it’s related to system behavior of object initialization and concurrency; any pointers will be highly appreciated.
Part of my application source code:
class ProductAdminForm(BaseDynamicEntityForm):
"""
This is an admin form which uses the EAV class
"""
model = models.Product
def _build_dynamic_fields(self):
"""
this is an integration point, overrides existing method in third party source
:return:
"""
# reset form fields
self.fields = deepcopy(self.base_fields)
# Refactored for test:
#product_template = self.initial.get('product_template') or self.instance.product_template
#
option_1 = self.initial.get('product_template')
option_2 = self.instance.product_template
product_template = option_1 or option_2
# this line is the primary reason for override, additional filter on the fields by business
# Refactored for test:
#for attribute in self.entity.get_all_attributes().filter(producttemplateattribute__product_template=product_template):
#
all_attributes = self.entity.get_all_attributes()
the_attributes = all_attributes.filter(producttemplateattribute__product_template=product_template)
for attribute in the_attributes:
value = getattr(self.entity, attribute.slug)
... ...
return
Error message:
[18/Dec/2020 14:18:27] ERROR [django.request:230] Internal Server Error: /api/admin/APPLICATION/product/add/
Traceback (most recent call last):
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/contrib/admin/options.py", line 614, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 233, in inner
return view(request, *args, **kwargs)
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1653, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1534, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1573, in _changeform_view
form_validated = form.is_valid()
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/forms/forms.py", line 177, in is_valid
return self.is_bound and not self.errors
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/forms/forms.py", line 172, in errors
self.full_clean()
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/forms/forms.py", line 374, in full_clean
self._clean_fields()
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/forms/forms.py", line 392, in _clean_fields
value = field.clean(value)
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/forms/fields.py", line 149, in clean
value = self.to_python(value)
File "/data/APP-py3/venv3.7/lib/python3.7/site-packages/django/forms/fields.py", line 471, in to_python
result = parse_datetime(value.strip())
AttributeError: 'list' object has no attribute 'strip'