mirror of
https://github.com/openSUSE/osc.git
synced 2025-01-06 06:46:14 +01:00
Fix exporting an empty model field in BaseModel.dict()
This commit is contained in:
parent
3baeb4b64e
commit
b4a1b29f57
@ -339,8 +339,10 @@ class BaseModel(metaclass=ModelMeta):
|
|||||||
for name, field in self.__fields__.items():
|
for name, field in self.__fields__.items():
|
||||||
if field.exclude:
|
if field.exclude:
|
||||||
continue
|
continue
|
||||||
if field.is_model:
|
value = getattr(self, name)
|
||||||
result[name] = getattr(self, name).dict()
|
if value is not None and field.is_model:
|
||||||
|
result[name] = value.dict()
|
||||||
else:
|
else:
|
||||||
result[name] = getattr(self, name)
|
result[name] = value
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
@ -186,14 +186,17 @@ class Test(unittest.TestCase):
|
|||||||
self.assertEqual(field.is_optional, True)
|
self.assertEqual(field.is_optional, True)
|
||||||
self.assertEqual(field.origin_type, TestSubmodel)
|
self.assertEqual(field.origin_type, TestSubmodel)
|
||||||
self.assertEqual(m.field, None)
|
self.assertEqual(m.field, None)
|
||||||
|
m.dict()
|
||||||
|
|
||||||
m = TestModel(field=TestSubmodel())
|
m = TestModel(field=TestSubmodel())
|
||||||
self.assertIsInstance(m.field, TestSubmodel)
|
self.assertIsInstance(m.field, TestSubmodel)
|
||||||
self.assertEqual(m.field.text, "default")
|
self.assertEqual(m.field.text, "default")
|
||||||
|
m.dict()
|
||||||
|
|
||||||
m = TestModel(field={"text": "text"})
|
m = TestModel(field={"text": "text"})
|
||||||
self.assertNotEqual(m.field, None)
|
self.assertNotEqual(m.field, None)
|
||||||
self.assertEqual(m.field.text, "text")
|
self.assertEqual(m.field.text, "text")
|
||||||
|
m.dict()
|
||||||
|
|
||||||
def test_enum(self):
|
def test_enum(self):
|
||||||
class Numbers(Enum):
|
class Numbers(Enum):
|
||||||
|
Loading…
Reference in New Issue
Block a user