<h2 align="center">
SQLModelX
</h2>
<p align="center">
<em>SQLModelX is an extension of the SQLModel library.</em><br/>
</p>
<p align="center">
<a href="https://github.com/amisadmin/sqlmodelx/actions/workflows/pytest.yml" target="_blank">
<img src="https://github.com/amisadmin/sqlmodelx/actions/workflows/pytest.yml/badge.svg" alt="Pytest">
</a>
<a href="https://pypi.org/project/sqlmodelx" target="_blank">
<img src="https://badgen.net/pypi/v/sqlmodelx?color=blue" alt="Package version">
</a>
<a href="https://gitter.im/amisadmin/fastapi-amis-admin">
<img src="https://badges.gitter.im/amisadmin/fastapi-amis-admin.svg" alt="Chat on Gitter"/>
</a>
<a href="https://jq.qq.com/?_wv=1027&k=U4Dv6x8W" target="_blank">
<img src="https://badgen.net/badge/qq%E7%BE%A4/229036692/orange" alt="229036692">
</a>
</p>
## Install
```bash
pip install sqlmodelx
```
## Usage
```python
from datetime import datetime
from typing import List,Optional
from sqlmodel import Field, Relationship, Session, select
from sqlmodelx import SQLModel
from sqlmodelx.main import SQLModelMetaclass
class PkMixin(SQLModel):
id: Optional[int] = Field(default = None, primary_key = True, nullable = False)
class BaseUser(PkMixin):
username: str = Field(default = '', nullable = False)
password: str = Field(default = '', nullable = False)
create_time: datetime = Field(default_factory = datetime.now, nullable = False)
group_id: Optional[int] = Field(default = None, nullable = True, foreign_key = 'group.id')
class User(BaseUser, table = True):
__tablename__ = 'user'
group: Optional['Group'] = Relationship(back_populates = 'users')
class Group(SQLModel, table = True):
id: Optional[int] = Field(default = None, primary_key = True, nullable = False)
name: str = Field(default = '', nullable = False)
create_time: datetime = Field(default_factory = datetime.now, nullable = False)
users: List[User] = Relationship(
back_populates = 'group',
sa_relationship_kwargs = {"enable_typechecks": False}
)
def test_class_and_metaclass(engine):
"""Test class and metaclass"""
from sqlmodel import SQLModel as _SQLModel
from sqlmodel.main import SQLModelMetaclass as _SQLModelMetaclass
assert isinstance(User, SQLModelMetaclass)
assert isinstance(User, _SQLModelMetaclass)
assert issubclass(User, SQLModel)
assert issubclass(User, _SQLModel)
def test_base_is_table_and_subclass_is_table(engine):
"""Test base class and subclass are both ORM database tables"""
# Extend the user ORM model to add a field
class NickNameUser(User, table = True):
nickname: str = Field(default = '')
# Extend the user ORM model to add a field
class AvatarUser(NickNameUser, table = True):
avatar: str = Field(default = '')
# Create the database tables
SQLModel.metadata.drop_all(engine)
SQLModel.metadata.create_all(engine)
avatar_user = AvatarUser(
username = "Deadpond",
password = "Dive Wilson",
nickname = 'nickname',
avatar = 'avatar',
group = Group(name = 'admin'),
)
with Session(engine) as session:
session.add(avatar_user)
session.commit()
session.refresh(avatar_user)
assert avatar_user.id is not None
# The relationship property of the base class will also be inherited
assert avatar_user.group.id is not None
nickname_user = session.query(NickNameUser).first()
assert nickname_user.nickname == avatar_user.nickname
# The relationship property of the base class will also be inherited
assert nickname_user.group.id == avatar_user.group.id
user = session.exec(select(User)).first()
assert user.username == avatar_user.username
assert user.group.id == avatar_user.group.id
def test_base_is_table_and_subclass_is_not_table(engine):
"""Test base class is an ORM database table, the subclass is not"""
# Create a pydantic model quickly through inheritance
class NickNameUserSchema(User, table = False):
nickname: str = Field(default = '')
user = User(
username = "Deadpond",
password = "Dive Wilson",
group = Group(name = 'admin')
)
with Session(engine) as session:
session.add(user)
session.commit()
session.refresh(user)
assert user.id is not None
assert user.group.id is not None
user_ex = NickNameUserSchema.from_orm(user)
assert user_ex.id == user.id
```
## License
According to the `Apache2.0` protocol.
Raw data
{
"_id": null,
"home_page": null,
"name": "sqlmodelx",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "sqlmodelx, sqlmodel, sqlalchemy, fastapi, fastapi-amis-admin",
"author": null,
"author_email": "atomi <1456417373@qq.com>",
"download_url": "https://files.pythonhosted.org/packages/35/9e/8c750a37d5175301a861349153a67426638f053e77ccc75239dd21e7cd5c/sqlmodelx-0.0.12.tar.gz",
"platform": null,
"description": "<h2 align=\"center\">\n SQLModelX\n</h2>\n<p align=\"center\">\n <em>SQLModelX is an extension of the SQLModel library.</em><br/>\n</p>\n<p align=\"center\">\n <a href=\"https://github.com/amisadmin/sqlmodelx/actions/workflows/pytest.yml\" target=\"_blank\">\n <img src=\"https://github.com/amisadmin/sqlmodelx/actions/workflows/pytest.yml/badge.svg\" alt=\"Pytest\">\n </a>\n <a href=\"https://pypi.org/project/sqlmodelx\" target=\"_blank\">\n <img src=\"https://badgen.net/pypi/v/sqlmodelx?color=blue\" alt=\"Package version\">\n </a>\n <a href=\"https://gitter.im/amisadmin/fastapi-amis-admin\">\n <img src=\"https://badges.gitter.im/amisadmin/fastapi-amis-admin.svg\" alt=\"Chat on Gitter\"/>\n </a>\n <a href=\"https://jq.qq.com/?_wv=1027&k=U4Dv6x8W\" target=\"_blank\">\n <img src=\"https://badgen.net/badge/qq%E7%BE%A4/229036692/orange\" alt=\"229036692\">\n </a>\n</p>\n\n## Install\n\n```bash\npip install sqlmodelx\n```\n\n## Usage\n\n```python\nfrom datetime import datetime\nfrom typing import List,Optional\n\nfrom sqlmodel import Field, Relationship, Session, select\n\nfrom sqlmodelx import SQLModel\nfrom sqlmodelx.main import SQLModelMetaclass\n\nclass PkMixin(SQLModel):\n id: Optional[int] = Field(default = None, primary_key = True, nullable = False)\n\nclass BaseUser(PkMixin):\n username: str = Field(default = '', nullable = False)\n password: str = Field(default = '', nullable = False)\n create_time: datetime = Field(default_factory = datetime.now, nullable = False)\n group_id: Optional[int] = Field(default = None, nullable = True, foreign_key = 'group.id')\n\nclass User(BaseUser, table = True):\n __tablename__ = 'user'\n group: Optional['Group'] = Relationship(back_populates = 'users')\n\nclass Group(SQLModel, table = True):\n id: Optional[int] = Field(default = None, primary_key = True, nullable = False)\n name: str = Field(default = '', nullable = False)\n create_time: datetime = Field(default_factory = datetime.now, nullable = False)\n users: List[User] = Relationship(\n back_populates = 'group',\n sa_relationship_kwargs = {\"enable_typechecks\": False}\n )\n\ndef test_class_and_metaclass(engine):\n \"\"\"Test class and metaclass\"\"\"\n from sqlmodel import SQLModel as _SQLModel\n from sqlmodel.main import SQLModelMetaclass as _SQLModelMetaclass\n\n assert isinstance(User, SQLModelMetaclass)\n assert isinstance(User, _SQLModelMetaclass)\n assert issubclass(User, SQLModel)\n assert issubclass(User, _SQLModel)\n\ndef test_base_is_table_and_subclass_is_table(engine):\n \"\"\"Test base class and subclass are both ORM database tables\"\"\"\n\n # Extend the user ORM model to add a field\n class NickNameUser(User, table = True):\n nickname: str = Field(default = '')\n\n # Extend the user ORM model to add a field\n class AvatarUser(NickNameUser, table = True):\n avatar: str = Field(default = '')\n\n # Create the database tables\n SQLModel.metadata.drop_all(engine)\n SQLModel.metadata.create_all(engine)\n\n avatar_user = AvatarUser(\n username = \"Deadpond\",\n password = \"Dive Wilson\",\n nickname = 'nickname',\n avatar = 'avatar',\n group = Group(name = 'admin'),\n )\n\n with Session(engine) as session:\n session.add(avatar_user)\n session.commit()\n session.refresh(avatar_user)\n assert avatar_user.id is not None\n # The relationship property of the base class will also be inherited\n assert avatar_user.group.id is not None\n\n nickname_user = session.query(NickNameUser).first()\n assert nickname_user.nickname == avatar_user.nickname\n # The relationship property of the base class will also be inherited\n assert nickname_user.group.id == avatar_user.group.id\n\n user = session.exec(select(User)).first()\n assert user.username == avatar_user.username\n assert user.group.id == avatar_user.group.id\n\ndef test_base_is_table_and_subclass_is_not_table(engine):\n \"\"\"Test base class is an ORM database table, the subclass is not\"\"\"\n\n # Create a pydantic model quickly through inheritance\n class NickNameUserSchema(User, table = False):\n nickname: str = Field(default = '')\n\n user = User(\n username = \"Deadpond\",\n password = \"Dive Wilson\",\n group = Group(name = 'admin')\n )\n\n with Session(engine) as session:\n session.add(user)\n session.commit()\n session.refresh(user)\n assert user.id is not None\n assert user.group.id is not None\n\n user_ex = NickNameUserSchema.from_orm(user)\n assert user_ex.id == user.id\n\n```\n\n## License\n\nAccording to the `Apache2.0` protocol.\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "SQLModelX is an extension of the SQLModel library.",
"version": "0.0.12",
"project_urls": {
"FastAPI-Amis-Admin": "https://github.com/amisadmin/fastapi_amis_admin",
"Source": "https://github.com/amisadmin/sqlmodelx"
},
"split_keywords": [
"sqlmodelx",
" sqlmodel",
" sqlalchemy",
" fastapi",
" fastapi-amis-admin"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c83ab4f6639b248e9bb3dc3154b12c84c2353bbeace71a4e07561019ca47d217",
"md5": "08b0c89010db909aab9ea83a80c7f33f",
"sha256": "0c089bb83787d6d88a926d12b4b8e3d794ad39eff1a5b8724fb0775b21bc75f5"
},
"downloads": -1,
"filename": "sqlmodelx-0.0.12-py3-none-any.whl",
"has_sig": false,
"md5_digest": "08b0c89010db909aab9ea83a80c7f33f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 8741,
"upload_time": "2024-03-24T07:59:57",
"upload_time_iso_8601": "2024-03-24T07:59:57.177359Z",
"url": "https://files.pythonhosted.org/packages/c8/3a/b4f6639b248e9bb3dc3154b12c84c2353bbeace71a4e07561019ca47d217/sqlmodelx-0.0.12-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "359e8c750a37d5175301a861349153a67426638f053e77ccc75239dd21e7cd5c",
"md5": "3f858a13cd04f3898c8027c462a2f74b",
"sha256": "5defd126e48f83e032329656a15aacc8968affc51477d48f245d1fc592d2ae9c"
},
"downloads": -1,
"filename": "sqlmodelx-0.0.12.tar.gz",
"has_sig": false,
"md5_digest": "3f858a13cd04f3898c8027c462a2f74b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 11661,
"upload_time": "2024-03-24T07:59:58",
"upload_time_iso_8601": "2024-03-24T07:59:58.504334Z",
"url": "https://files.pythonhosted.org/packages/35/9e/8c750a37d5175301a861349153a67426638f053e77ccc75239dd21e7cd5c/sqlmodelx-0.0.12.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-24 07:59:58",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "amisadmin",
"github_project": "fastapi_amis_admin",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "sqlmodelx"
}