-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
105 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .fields import ObjectIdField | ||
|
||
__all__ = ["ObjectIdField"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from bson import ObjectId | ||
from bson.errors import InvalidId | ||
from django.core.exceptions import ValidationError | ||
from django.forms import Field | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
|
||
class ObjectIdField(Field): | ||
default_error_messages = { | ||
"invalid": _("Enter a valid Object Id."), | ||
} | ||
|
||
def prepare_value(self, value): | ||
if isinstance(value, ObjectId): | ||
return str(value) | ||
return value | ||
|
||
def to_python(self, value): | ||
value = super().to_python(value) | ||
if value in self.empty_values: | ||
return None | ||
if not isinstance(value, ObjectId): | ||
try: | ||
value = ObjectId(value) | ||
except InvalidId: | ||
raise ValidationError(self.error_messages["invalid"], code="invalid") from None | ||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Forms API reference | ||
=================== | ||
|
||
.. module:: django_mongodb.forms | ||
|
||
Some MongoDB-specific fields are available in ``django_mongodb.forms``. | ||
|
||
``ObjectIdField`` | ||
----------------- | ||
|
||
.. class:: ObjectIdField | ||
|
||
Stores an :class:`~bson.objectid.ObjectId`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ django-mongodb 5.0.x documentation | |
|
||
fields | ||
querysets | ||
forms | ||
|
||
Indices and tables | ||
================== | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from bson import ObjectId | ||
from django.core.exceptions import ValidationError | ||
from django.test import SimpleTestCase | ||
|
||
from django_mongodb.forms.fields import ObjectIdField | ||
|
||
|
||
class ObjectIdFieldTests(SimpleTestCase): | ||
def test_clean(self): | ||
field = ObjectIdField() | ||
value = field.clean("675747ec45260945758d76bc") | ||
self.assertEqual(value, ObjectId("675747ec45260945758d76bc")) | ||
|
||
def test_clean_objectid(self): | ||
field = ObjectIdField() | ||
value = field.clean(ObjectId("675747ec45260945758d76bc")) | ||
self.assertEqual(value, ObjectId("675747ec45260945758d76bc")) | ||
|
||
def test_clean_empty_string(self): | ||
field = ObjectIdField(required=False) | ||
value = field.clean("") | ||
self.assertEqual(value, None) | ||
|
||
def test_clean_invalid(self): | ||
field = ObjectIdField() | ||
with self.assertRaises(ValidationError) as cm: | ||
field.clean("invalid") | ||
self.assertEqual(cm.exception.messages[0], "Enter a valid Object Id.") | ||
|
||
def test_prepare_value(self): | ||
field = ObjectIdField() | ||
value = field.prepare_value(ObjectId("675747ec45260945758d76bc")) | ||
self.assertEqual(value, "675747ec45260945758d76bc") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters