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

DatetimeField Field identification error #1808

Open
phw50365036 opened this issue Dec 12, 2024 · 1 comment
Open

DatetimeField Field identification error #1808

phw50365036 opened this issue Dec 12, 2024 · 1 comment

Comments

@phw50365036
Copy link

Describe the bug
I have a project that is a table created by django with a time field called models.DateTimeField that records the time in the database as follows: 2022-07-29 11:00:24.794
I created a new fastapi project, using tortoise-orm to connect to Oracle database
There are two problems:

  1. When I define the time field in the model, if I define the field like this:
    EDIT_DATE = fields.DatetimeField(description=' Modify date ')
    The following error is reported:
    Traceback (most recent call last):
    File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 693, in lifespan
    async with self.lifespan_context(app) as maybe_state:
    File "/usr/lib64/python3.11/contextlib.py", line 210, in aenter
    return await anext(self.gen)
    ^^^^^^^^^^^^^^^^^^^^^
    File "/home/whatsup_alarm/core/event.py", line 29, in lifespan
    await get_whatsup_servers_information()
    File "/home/whatsup_alarm/core/event.py", line 41, in get_whatsup_servers_information
    objs = await NetDeviceSysMonitor.filter(**filter_par)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/tortoise/queryset.py", line 1151, in _execute
    instance_list = await self._db.executor_class(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/tortoise/backends/base/executor.py", line 139, in execute_select
    instance: "Model" = self.model._init_from_db(**dict(row_items[:current_idx]))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/tortoise/models.py", line 754, in _init_from_db
    setattr(self, model_field, field.to_python_value(kwargs[key]))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/tortoise/fields/data.py", line 354, in to_python_value
    value = parse_datetime(value)
    ^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/iso8601/iso8601.py", line 124, in parse_date
    raise ParseError(e)
    iso8601.iso8601.ParseError: expected string or bytes-like object, got 'datetime.date'

If I define this field like this:
EDIT_DATE = fields.DateField(description=' Modify date ')
I can only get the date information, not the time information

If I define this field like this:
EDIT_DATE = fields.TimeField(description=' Modify date ')
The following error is reported:
ERROR: Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 693, in lifespan
async with self.lifespan_context(app) as maybe_state:
File "/usr/lib64/python3.11/contextlib.py", line 210, in aenter
return await anext(self.gen)
^^^^^^^^^^^^^^^^^^^^^
File "/home/whatsup_alarm/core/event.py", line 29, in lifespan
await get_whatsup_servers_information()
File "/home/whatsup_alarm/core/event.py", line 41, in get_whatsup_servers_information
objs = await NetDeviceSysMonitor.filter(**filter_par)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/tortoise/queryset.py", line 1151, in _execute
instance_list = await self._db.executor_class(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/tortoise/backends/base/executor.py", line 139, in execute_select
instance: "Model" = self.model._init_from_db(**dict(row_items[:current_idx]))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/tortoise/models.py", line 754, in _init_from_db
setattr(self, model_field, field.to_python_value(kwargs[key]))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/tortoise/fields/data.py", line 444, in to_python_value
if timezone.is_naive(value):
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/tortoise/timezone.py", line 84, in is_naive
return value.utcoffset() is None
^^^^^^^^^^^^^^^
AttributeError: 'datetime.date' object has no attribute 'utcoffset'

  1. The field names in the database are all uppercase. When I use lowercase field names in the model definition, I get an error, indicating that the field name is illegal
@phw50365036
Copy link
Author

Did I misdescribe the problem? Why is there no response

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

No branches or pull requests

1 participant