From a25c0fc76b5ef3822d55602fa4bea22b880984a0 Mon Sep 17 00:00:00 2001 From: long2ice Date: Wed, 3 Feb 2021 22:39:41 +0800 Subject: [PATCH] Add more for describe of `ManyToManyField` --- tests/utils/test_describe_model.py | 93 +++++++++++++++++++++++++----- tortoise/fields/relational.py | 11 ++++ 2 files changed, 90 insertions(+), 14 deletions(-) diff --git a/tests/utils/test_describe_model.py b/tests/utils/test_describe_model.py index 3f3ffae90..c5468d27c 100644 --- a/tests/utils/test_describe_model.py +++ b/tests/utils/test_describe_model.py @@ -135,7 +135,6 @@ def test_describe_models_some(self): def test_describe_model_straight(self): val = StraightFields.describe() - self.assertEqual( val, { @@ -315,6 +314,13 @@ def test_describe_model_straight(self): "description": "M2M to myself", "docstring": None, "constraints": {}, + "model_name": "models.StraightFields", + "related_name": "rel_from", + "forward_key": "straightfields_id", + "backward_key": "straightfields_rel_id", + "through": "straightfields_straightfields", + "on_delete": "CASCADE", + "_generated": False, }, { "name": "rel_from", @@ -328,6 +334,13 @@ def test_describe_model_straight(self): "description": "M2M to myself", "docstring": None, "constraints": {}, + "model_name": "models.StraightFields", + "related_name": "rel_to", + "forward_key": "straightfields_rel_id", + "backward_key": "straightfields_id", + "through": "straightfields_straightfields", + "on_delete": "CASCADE", + "_generated": True, }, ], }, @@ -335,7 +348,6 @@ def test_describe_model_straight(self): def test_describe_model_straight_native(self): val = StraightFields.describe(serializable=False) - self.assertEqual( val, { @@ -515,6 +527,13 @@ def test_describe_model_straight_native(self): "description": "M2M to myself", "docstring": None, "constraints": {}, + "model_name": "models.StraightFields", + "related_name": "rel_from", + "forward_key": "straightfields_id", + "backward_key": "straightfields_rel_id", + "through": "straightfields_straightfields", + "on_delete": "CASCADE", + "_generated": False, }, { "name": "rel_from", @@ -528,6 +547,13 @@ def test_describe_model_straight_native(self): "description": "M2M to myself", "docstring": None, "constraints": {}, + "model_name": "models.StraightFields", + "related_name": "rel_to", + "forward_key": "straightfields_rel_id", + "backward_key": "straightfields_id", + "through": "straightfields_straightfields", + "on_delete": "CASCADE", + "_generated": True, }, ], }, @@ -535,7 +561,6 @@ def test_describe_model_straight_native(self): def test_describe_model_source(self): val = SourceFields.describe() - self.assertEqual( val, { @@ -715,6 +740,13 @@ def test_describe_model_source(self): "description": "M2M to myself", "docstring": None, "constraints": {}, + "model_name": "models.SourceFields", + "related_name": "rel_from", + "forward_key": "sts_forward", + "backward_key": "backward_sts", + "through": "sometable_self", + "on_delete": "CASCADE", + "_generated": False, }, { "name": "rel_from", @@ -728,6 +760,13 @@ def test_describe_model_source(self): "description": "M2M to myself", "docstring": None, "constraints": {}, + "model_name": "models.SourceFields", + "related_name": "rel_to", + "forward_key": "backward_sts", + "backward_key": "sts_forward", + "through": "sometable_self", + "on_delete": "CASCADE", + "_generated": True, }, ], }, @@ -735,7 +774,6 @@ def test_describe_model_source(self): def test_describe_model_source_native(self): val = SourceFields.describe(serializable=False) - self.assertEqual( val, { @@ -915,6 +953,13 @@ def test_describe_model_source_native(self): "description": "M2M to myself", "docstring": None, "constraints": {}, + "model_name": "models.SourceFields", + "related_name": "rel_from", + "forward_key": "sts_forward", + "backward_key": "backward_sts", + "through": "sometable_self", + "on_delete": "CASCADE", + "_generated": False, }, { "name": "rel_from", @@ -928,6 +973,13 @@ def test_describe_model_source_native(self): "description": "M2M to myself", "docstring": None, "constraints": {}, + "model_name": "models.SourceFields", + "related_name": "rel_to", + "forward_key": "backward_sts", + "backward_key": "sts_forward", + "through": "sometable_self", + "on_delete": "CASCADE", + "_generated": True, }, ], }, @@ -982,17 +1034,24 @@ def test_describe_model_uuidpk(self): "backward_o2o_fields": [], "m2m_fields": [ { - "name": "peers", + "_generated": True, + "backward_key": "uuidpkmodel_id", + "constraints": {}, + "default": None, + "description": None, + "docstring": None, "field_type": "ManyToManyFieldInstance", - "python_type": "models.UUIDM2MRelatedModel", + "forward_key": "uuidm2mrelatedmodel_id", "generated": False, + "indexed": False, + "model_name": "models.UUIDM2MRelatedModel", + "name": "peers", "nullable": False, + "on_delete": "CASCADE", + "python_type": "models.UUIDM2MRelatedModel", + "related_name": "models", + "through": "uuidm2mrelatedmodel_uuidpkmodel", "unique": False, - "indexed": False, - "default": None, - "description": None, - "docstring": None, - "constraints": {}, } ], }, @@ -1000,7 +1059,6 @@ def test_describe_model_uuidpk(self): def test_describe_model_uuidpk_native(self): val = UUIDPkModel.describe(serializable=False) - self.assertEqual( val, { @@ -1048,16 +1106,23 @@ def test_describe_model_uuidpk_native(self): "m2m_fields": [ { "name": "peers", - "field_type": ManyToManyFieldInstance, - "python_type": UUIDM2MRelatedModel, "generated": False, "nullable": False, + "field_type": ManyToManyFieldInstance, + "python_type": UUIDM2MRelatedModel, "unique": False, "indexed": False, "default": None, "description": None, "docstring": None, "constraints": {}, + "model_name": "models.UUIDM2MRelatedModel", + "related_name": "models", + "forward_key": "uuidm2mrelatedmodel_id", + "backward_key": "uuidpkmodel_id", + "through": "uuidm2mrelatedmodel_uuidpkmodel", + "on_delete": "CASCADE", + "_generated": True, } ], }, diff --git a/tortoise/fields/relational.py b/tortoise/fields/relational.py index 10c0b65e6..82afb2067 100644 --- a/tortoise/fields/relational.py +++ b/tortoise/fields/relational.py @@ -400,6 +400,17 @@ def __init__( self._generated: bool = False self.on_delete = on_delete + def describe(self, serializable: bool) -> dict: + desc = super().describe(serializable) + desc["model_name"] = self.model_name + desc["related_name"] = self.related_name + desc["forward_key"] = self.forward_key + desc["backward_key"] = self.backward_key + desc["through"] = self.through + desc["on_delete"] = self.on_delete + desc["_generated"] = self._generated + return desc + def OneToOneField( model_name: str,