Skip to content

Commit 97a421a

Browse files
committed
store timezone aware datetimes instead of strings
refs #11
1 parent c97bb15 commit 97a421a

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ DATABASES = {
5454

5555
- Queries with joins aren't supported.
5656

57+
- `DateTimeField` doesn't support microsecond precision.
58+
5759
## Troubleshooting
5860

5961
TODO

django_mongodb/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ def _connect(self):
9191
self.operation_flags = {"save": flags, "delete": flags, "update": flags}
9292

9393
self.connection = MongoClient(
94-
host=settings_dict["HOST"] or None, port=int(settings_dict["PORT"] or 27017), **options
94+
host=settings_dict["HOST"] or None,
95+
port=int(settings_dict["PORT"] or 27017),
96+
tz_aware=True,
97+
**options,
9598
)
9699
db_name = settings_dict["NAME"]
97100
if db_name:

django_mongodb/features.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,13 @@ class DatabaseFeatures(BaseDatabaseFeatures):
158158
"timezones.tests.NewDatabaseTests.test_cursor_explicit_time_zone",
159159
"timezones.tests.NewDatabaseTests.test_raw_sql",
160160
},
161+
"BSON Date type doesn't support microsecond precision.": {
162+
"basic.tests.ModelRefreshTests.test_refresh_unsaved",
163+
"basic.tests.ModelTest.test_microsecond_precision",
164+
"timezones.tests.LegacyDatabaseTests.test_auto_now_and_auto_now_add",
165+
"timezones.tests.LegacyDatabaseTests.test_aware_datetime_in_local_timezone_with_microsecond",
166+
"timezones.tests.LegacyDatabaseTests.test_naive_datetime_with_microsecond",
167+
"timezones.tests.NewDatabaseTests.test_aware_datetime_in_local_timezone_with_microsecond",
168+
"timezones.tests.NewDatabaseTests.test_naive_datetime_with_microsecond",
169+
},
161170
}

django_mongodb/operations.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@
1010
class DatabaseOperations(BaseDatabaseOperations):
1111
compiler_module = "django_mongodb.compiler"
1212

13+
def adapt_datetimefield_value(self, value):
14+
if not settings.USE_TZ and timezone.is_naive(value):
15+
value = timezone.make_aware(value)
16+
return value
17+
1318
def get_db_converters(self, expression):
1419
converters = super().get_db_converters(expression)
1520
internal_type = expression.output_field.get_internal_type()
1621
if internal_type == "DateField":
1722
converters.append(self.convert_datefield_value)
1823
elif internal_type == "DateTimeField":
19-
converters.append(self.convert_datetimefield_value)
24+
if not settings.USE_TZ:
25+
converters.append(self.convert_datetimefield_value)
2026
elif internal_type == "DecimalField":
2127
converters.append(self.convert_decimalfield_value)
2228
elif internal_type == "TimeField":
@@ -32,10 +38,8 @@ def convert_datefield_value(self, value, expression, connection):
3238

3339
def convert_datetimefield_value(self, value, expression, connection):
3440
if value is not None:
35-
value = datetime.datetime.fromisoformat(value)
36-
if not settings.USE_TZ and timezone.is_aware(value):
37-
# Django expects naive datetimes when settings.USE_TZ is False.
38-
value = timezone.make_naive(value)
41+
# Django expects naive datetimes when settings.USE_TZ is False.
42+
value = timezone.make_naive(value)
3943
return value
4044

4145
def convert_decimalfield_value(self, value, expression, connection):

0 commit comments

Comments
 (0)