diff --git a/.coverage b/.coverage index 997d622..09d9c48 100644 Binary files a/.coverage and b/.coverage differ diff --git a/orm/fields.py b/orm/fields.py index 4393bd1..5a28ecf 100644 --- a/orm/fields.py +++ b/orm/fields.py @@ -193,6 +193,8 @@ class ForeignKey(BaseField): def expand_relationship(self, value, child): if isinstance(value, self.to): model = value + elif isinstance(value, dict): + model = self.to(**value) else: model = self.to(**{self.to.__pkname__: value}) diff --git a/orm/models.py b/orm/models.py index 7eca3b9..05bf654 100644 --- a/orm/models.py +++ b/orm/models.py @@ -315,7 +315,7 @@ class Model(tuple, metaclass=ModelMetaclass): objects = QuerySet() - def __init__(self, **kwargs) -> None: + def __init__(self, *args, **kwargs) -> None: self._orm_id = uuid.uuid4().hex self._orm_saved = False self._orm_relationship_manager = RelationshipManager(self) diff --git a/tests/test_fastapi_usage.py b/tests/test_fastapi_usage.py index ae647a0..67a4c5f 100644 --- a/tests/test_fastapi_usage.py +++ b/tests/test_fastapi_usage.py @@ -16,6 +16,15 @@ database = databases.Database(DATABASE_URL, force_rollback=True) metadata = sqlalchemy.MetaData() +class Category(orm.Model): + __tablename__ = "cateries" + __metadata__ = metadata + __database__ = database + + id = orm.Integer(primary_key=True) + name = orm.String(length=100) + + class Item(orm.Model): __tablename__ = "users" __metadata__ = metadata @@ -23,6 +32,7 @@ class Item(orm.Model): id = orm.Integer(primary_key=True) name = orm.String(length=100) + category = orm.ForeignKey(Category, nullable=True) @app.post("/items/", response_model=Item) @@ -34,9 +44,8 @@ client = TestClient(app) def test_read_main(): - response = client.post("/items/", json={'name': 'test', 'id': 1}) - print(response.json()) + response = client.post("/items/", json={'name': 'test', 'id': 1, 'category': {'name': 'test cat'}}) assert response.status_code == 200 - assert response.json() == {'name': 'test', 'id': 1} + assert response.json() == {'category': {'id': None, 'name': 'test cat'}, 'id': 1, 'name': 'test'} item = Item(**response.json()) assert item.id == 1