Skip to content

Commit 1c1a947

Browse files
committed
Refactor objectId fields.
1 parent 860b444 commit 1c1a947

File tree

4 files changed

+21
-27
lines changed

4 files changed

+21
-27
lines changed

django_mongodb/fields/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .auto import ObjectIdAutoField
22
from .duration import register_duration_field
33
from .json import register_json_field
4-
from .object_id import ObjectIdField
4+
from .objectid import ObjectIdField
55

66
__all__ = ["register_fields", "ObjectIdAutoField", "ObjectIdField"]
77

django_mongodb/fields/auto.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
from django.core import exceptions
33
from django.db.models.fields import AutoField
44
from django.utils.functional import cached_property
5-
from django.utils.translation import gettext_lazy as _
65

6+
from .objectid_mixin import ObjectIdMixin
77

8-
class ObjectIdAutoField(AutoField):
9-
default_error_messages = {
10-
"invalid": _("“%(value)s” value must be an Object Id."),
11-
}
12-
description = _("Object Id")
138

9+
class ObjectIdAutoField(AutoField, ObjectIdMixin):
1410
def __init__(self, *args, **kwargs):
1511
kwargs["db_column"] = "_id"
1612
super().__init__(*args, **kwargs)
@@ -42,12 +38,6 @@ def get_prep_value(self, value):
4238
def get_internal_type(self):
4339
return "ObjectIdAutoField"
4440

45-
def db_type(self, connection):
46-
return "objectId"
47-
48-
def rel_db_type(self, connection):
49-
return "objectId"
50-
5141
def to_python(self, value):
5242
if value is None or isinstance(value, int):
5343
return value

django_mongodb/fields/object_id.py renamed to django_mongodb/fields/objectid.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,14 @@
22
from bson.errors import InvalidId
33
from django.core import exceptions
44
from django.db.models.fields import Field
5-
from django.utils.translation import gettext_lazy as _
65

6+
from .objectid_mixin import ObjectIdMixin
77

8-
class ObjectIdField(Field):
9-
default_error_messages = {
10-
"invalid": _("“%(value)s” value must be an Object Id."),
11-
}
12-
description = _("ObjectId")
138

9+
class ObjectIdField(Field, ObjectIdMixin):
1410
def get_internal_type(self):
1511
return "ObjectIdField"
1612

17-
def db_type(self, connection):
18-
return "objectId"
19-
20-
def rel_db_type(self, connection):
21-
return "objectId"
22-
2313
def to_python(self, value):
2414
try:
2515
return ObjectId(value)
@@ -32,6 +22,6 @@ def to_python(self, value):
3222

3323
def deconstruct(self):
3424
name, path, args, kwargs = super().deconstruct()
35-
if path.startswith("django_mongodb.fields.auto"):
36-
path = path.replace("django_mongodb.fields.auto", "django_mongodb.fields")
25+
if path.startswith("django_mongodb.fields.objectid"):
26+
path = path.replace("django_mongodb.fields.objectid", "django_mongodb.fields")
3727
return name, path, args, kwargs
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from django.utils.translation import gettext_lazy as _
2+
3+
4+
class ObjectIdMixin:
5+
default_error_messages = {
6+
"invalid": _("“%(value)s” value must be an Object Id."),
7+
}
8+
description = _("Object Id")
9+
10+
def db_type(self, connection):
11+
return "objectId"
12+
13+
def rel_db_type(self, connection):
14+
return "objectId"

0 commit comments

Comments
 (0)