-
Notifications
You must be signed in to change notification settings - Fork 198
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
Datetime with timezone edited incorrectly #796
Comments
I think you are looking at doing something like the following. This tells the backend to parse the datetime field considering the timezone offset: class UserAdmin(ModelView, model=User):
column_list = [User.id, User.name, User.created_at]
form_args = {"created_at": {"format": "%Y-%m-%dT%H:%M:%S.%f%z"}} And in your project root directory create ... copy content of sqladmin/templates/sqladmin/layout.html and append the following:
{% block tail %}
<script>
document.addEventListener('DOMContentLoaded', function () {
const dateTimeConfig = {
dateFormat: "Z", // This will output ISO 8601 format
};
flatpickr(".flatpickr-input", dateTimeConfig);
});
</script>
{% endblock %} Would get more complicated if you were using offsets other than UTC I suppose. EDIT Actually, there is a problem with the above.. Given the format TBH, it feels easier to handle this all server side and leave flatpickr config out of it. Here's something similar I've done to handle the from __future__ import annotations
from datetime import timezone
from typing import Any
from sqladmin import ModelView
from sqladmin.forms import ModelConverter, converts
from wtforms import DateTimeField
class DateTimeUTCField(DateTimeField):
def process_formdata(self, valuelist: list[Any]) -> None:
super().process_formdata(valuelist)
if self.data is None:
return
self.data = self.data.replace(tzinfo=timezone.utc)
class DateTimeUTCConverter(ModelConverter):
# mypy: error: Untyped decorator makes function "convert_date_time_utc" untyped [misc]
@converts("DateTimeUTC") # type: ignore[misc]
def convert_date_time_utc(self, *, kwargs: dict[str, Any], **_: Any) -> DateTimeUTCField: # noqa: PLR6301
return DateTimeUTCField(**kwargs)
class AuditModelView(ModelView):
form_converter = DateTimeUTCConverter |
Sorry for the late reply. Thank you for the workaround, it fixed the issue for me. |
Checklist
master
.Describe the bug
Datetime with timezone are edited incorrectly. In this example I have a table with a name and a created_at column:
If I change the name of a row, it will also edit the created_at column, most likely because the timezone is missing from the edit field:
You can see that the
created_at
column gets updated (from 9:55 to 7:55)Steps to reproduce the bug
Here is the complete program used in the example:
The issue doesn't happen with SQLite, only with PostgreSQL
Expected behavior
I expect the
created_at
column to not be modified.Actual behavior
The
created_at
column gets edited incorrectly (from 9:55 to 7:55)Debugging material
No response
Environment
sqladmin version: 0.18.0
Additional context
No response
The text was updated successfully, but these errors were encountered: