Python Forum
Upgraded Django, `AdminSplitDateTime` causes exception on `list` object - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Upgraded Django, `AdminSplitDateTime` causes exception on `list` object (/thread-32661.html)



Upgraded Django, `AdminSplitDateTime` causes exception on `list` object - 1780yz - Feb-24-2021

I'm upgrading a project from Python2.7 + Django1.8 to Python3.7 + Django3.1. The application contains a form using widget AdminSplitDateTime that displays a datetime attribute as two fields separately of date and time. This widget also splits one field into a list of two objects, and the default value is [None, None].

Root cause analysis:

I noticed that with Django1.8, function DateTimeField.to_python has a logic path to handle the list, see if isinstance(value, list): in the source code here.

However, the newer Django removed this handling, so result = parse_datetime(value.strip()) (see the source code here) triggers an exception AttributeError: 'list' object has no attribute 'strip'. At the triggering time, variable value was a list [None, None].

My questions: I want to keep the widget, or keep the two fields effect. Any hints will be highly appreciated.

Partial source code of my application:

class ProductAdminForm(BaseDynamicEntityForm):
    model = models.Product
    def _build_dynamic_fields(self):
        ...
        for attribute in self.entity.get_all_attributes()\
            .filter(producttemplateattribute__product_template=product_template):
            value = getattr(self.entity, attribute.slug)
            ...
            datatype = attribute.datatype
            ...
            elif datatype == attribute.TYPE_DATE:
                defaults.update({'widget': AdminSplitDateTime})
            ...



RE: Upgraded Django, `AdminSplitDateTime` causes exception on `list` object - michael1789 - Feb-24-2021

use a print(value = getattr(self.entity, attribute.slug)) to see what is returned. .strip() is a string function. And it says you are trying it on a list.

The answer is likely something like having to use the index (eg value[0].strip()) for each item in value.


RE: Upgraded Django, `AdminSplitDateTime` causes exception on `list` object - 1780yz - Feb-24-2021

Thank you for your reply, @michael1789.

The exception was triggered by result = parse_datetime(value.strip()) in source code here. I used a break point, and the value was [None, None].

Yes, you're right that it's not a string.

(Feb-24-2021, 04:16 PM)michael1789 Wrote: see what is returned. .strip() is a string function.