Skip to content

Commit

Permalink
Merge pull request #151 from slaarti/models_articles
Browse files Browse the repository at this point in the history
Models and Schemas, Batch 4: Articles, Authors, and Profiles
  • Loading branch information
igorbenav authored Sep 6, 2024
2 parents 4862832 + 030b4ed commit b8c16e9
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 97 deletions.
39 changes: 18 additions & 21 deletions docs/advanced/joins.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,29 +308,24 @@ The result will be:

##### Example

To demonstrate a one-to-many relationship, let's assume `User` and `Post` tables:
To demonstrate a one-to-many relationship, let's assume `Author` and `Article` tables:

```python
class User(Base):
__tablename__ = "user"
id = Column(Integer, primary key=True)
name = Column(String)

class Post(Base):
__tablename__ = "post"
id = Column(Integer, primary key=True)
user_id = Column(Integer, ForeignKey("user.id"))
content = Column(String)
--8<--
fastcrud/examples/author/model.py:model
fastcrud/examples/article/model.py:model
--8<--
```

Fetch a user and all their posts:

```python
user_posts = await user_crud.get_joined(
author_crud = FastCRUD(Author)
author_articles = await author_crud.get_joined(
db=db,
join_model=Post,
join_on=User.id == Post.user_id,
join_prefix="post_",
join_model=Article,
join_on=Author.id == Article.author_id,
join_prefix="article_",
join_type="left",
nest_joins=True,
id=1,
Expand All @@ -342,17 +337,19 @@ The result will be:
```json
{
"id": 1,
"name": "Example User",
"posts": [
"name": "Example Author",
"articles": [
{
"id": 101,
"user_id": 1,
"content": "First post content"
"author_id": 1,
"title": "First Article!",
"content": "First article content"
},
{
"id": 102,
"user_id": 1,
"content": "Second post content"
"author_id": 1,
"title": "Second Article?",
"content": "Second article content"
}
]
}
Expand Down
142 changes: 103 additions & 39 deletions fastcrud/crud/fast_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,62 @@ class FastCRUD(
--8<--
```
---
??? example "`profile/model.py`"
```python
--8<--
fastcrud/examples/profile/model.py:imports
fastcrud/examples/profile/model.py:model
--8<--
```
??? example "`profile/schemas.py`"
```python
--8<--
fastcrud/examples/profile/schemas.py:imports
fastcrud/examples/profile/schemas.py:readschema
--8<--
```
??? example "`author/model.py`"
```python
--8<--
fastcrud/examples/author/model.py:imports
fastcrud/examples/author/model.py:model
--8<--
```
??? example "`author/schemas.py`"
```python
--8<--
fastcrud/examples/author/schemas.py:imports
fastcrud/examples/author/schemas.py:readschema
--8<--
```
??? example "`article/model.py`"
```python
--8<--
fastcrud/examples/article/model.py:imports
fastcrud/examples/article/model.py:model
--8<--
```
??? example "`article/schemas.py`"
```python
--8<--
fastcrud/examples/article/schemas.py:imports
fastcrud/examples/article/schemas.py:readschema
--8<--
```
Example 1: Basic Usage
----------------------
Expand Down Expand Up @@ -1438,28 +1494,32 @@ async def get_joined(
```
Example using one-to-one relationship:
```python
result = await crud_user.get_joined(
author_crud = FastCRUD(Author)
result = await author_crud.get_joined(
db=session,
schema_to_select=UserSchema,
schema_to_select=ReadAuthorSchema,
join_model=Profile,
join_on=User.profile_id == Profile.id,
join_schema_to_select=ProfileSchema,
join_on=Author.profile_id == Profile.id,
join_schema_to_select=ReadProfileSchema,
nest_joins=True,
relationship_type='one-to-one', # note that this is the default behavior
)
# Expect 'result' to have 'profile' as a nested dictionary
```
Example using one-to-many relationship:
```python
result = await crud_user.get_joined(
result = await author_crud.get_joined(
db=session,
schema_to_select=UserSchema,
join_model=Post,
join_on=User.id == Post.user_id,
join_schema_to_select=PostSchema,
relationship_type='one-to-many',
schema_to_select=ReadAuthorSchema,
join_model=Article,
join_on=Author.id == Article.author_id,
join_schema_to_select=ReadArticleSchema,
nest_joins=True,
relationship_type='one-to-many',
)
# Expect 'result' to have 'posts' as a nested list of dictionaries
```
Expand Down Expand Up @@ -1784,36 +1844,40 @@ async def get_multi_joined(
)
```
Example using one-to-one relationship:
```python
users = await crud_user.get_multi_joined(
db=session,
schema_to_select=UserSchema,
join_model=Profile,
join_on=User.profile_id == Profile.id,
join_schema_to_select=ProfileSchema,
offset=0,
limit=10,
relationship_type='one-to-one', # note that this is the default behavior
)
# Expect 'profile' to be nested as a dictionary under each user
```
Example using one-to-one relationship:
Example using one-to-many relationship:
```python
users = await crud_user.get_multi_joined(
db=session,
schema_to_select=UserSchema,
join_model=Post,
join_on=User.id == Post.user_id,
join_schema_to_select=PostSchema,
nest_joins=True,
offset=0,
limit=10,
relationship_type='one-to-many',
)
# Expect 'posts' to be nested as a list of dictionaries under each user
```
```python
author_crud = FastCRUD(Author)
results = await author_crud.get_multi_joined(
db=session,
schema_to_select=ReadAuthorSchema,
join_model=Profile,
join_on=Author.profile_id == Profile.id,
join_schema_to_select=ReadProfileSchema,
nest_joins=True,
offset=0,
limit=10,
relationship_type='one-to-one', # note that this is the default behavior
)
# Expect 'profile' to be nested as a dictionary under each user
```
Example using one-to-many relationship:
```python
results = await author_crud.get_multi_joined(
db=session,
schema_to_select=ReadAuthorSchema,
join_model=Article,
join_on=Author.id == Article.author_id,
join_schema_to_select=ReadArticleSchema,
nest_joins=True,
offset=0,
limit=10,
relationship_type='one-to-many',
)
# Expect 'posts' to be nested as a list of dictionaries under each user
```
"""
if joins_config and (
join_model
Expand Down
Loading

0 comments on commit b8c16e9

Please sign in to comment.