-
Notifications
You must be signed in to change notification settings - Fork 14
Fix read custom model id field #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mahenzon
wants to merge
1
commit into
main
Choose a base branch
from
fix/read-custom-model-id-field
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
from sqlalchemy import String, Text | ||
from sqlalchemy.orm import Mapped, mapped_column, relationship | ||
|
||
from examples.api_for_sqlalchemy.models.base import BaseMetadata | ||
|
||
if TYPE_CHECKING: | ||
from examples.api_for_sqlalchemy.models.movie import Movie | ||
|
||
|
||
class AgeRating(BaseMetadata): | ||
__tablename__ = "age_rating" | ||
|
||
name: Mapped[str] = mapped_column( | ||
String(20), | ||
primary_key=True, | ||
) | ||
description: Mapped[str] = mapped_column( | ||
Text(), | ||
default="", | ||
server_default="", | ||
) | ||
|
||
movies: Mapped[list["Movie"]] = relationship( | ||
back_populates="age_rating_obj", | ||
) | ||
|
||
def __str__(self) -> str: | ||
return self.name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import TYPE_CHECKING, Optional | ||
|
||
from sqlalchemy import ( | ||
ForeignKey, | ||
Identity, | ||
Integer, | ||
String, | ||
Text, | ||
) | ||
from sqlalchemy.orm import ( | ||
Mapped, | ||
mapped_column, | ||
relationship, | ||
) | ||
|
||
from examples.api_for_sqlalchemy.models.base import Base | ||
|
||
if TYPE_CHECKING: | ||
from examples.api_for_sqlalchemy.models.age_rating import AgeRating | ||
|
||
|
||
class Movie(Base): | ||
__tablename__ = "movie" | ||
|
||
id: Mapped[int] = mapped_column( | ||
Integer, | ||
Identity(always=True), | ||
primary_key=True, | ||
autoincrement=True, | ||
) | ||
title: Mapped[str] = mapped_column( | ||
String(120), | ||
index=True, | ||
) | ||
description: Mapped[str] = mapped_column( | ||
Text, | ||
default="", | ||
server_default="", | ||
) | ||
age_rating: Mapped[Optional[str]] = mapped_column( | ||
ForeignKey( | ||
"age_rating.name", | ||
ondelete="SET NULL", | ||
), | ||
) | ||
age_rating_obj: Mapped["AgeRating"] = relationship( | ||
back_populates="movies", | ||
) | ||
|
||
def __str__(self) -> str: | ||
return self.title | ||
|
||
def __repr__(self) -> str: | ||
return f"Movie(id={self.id}, title={self.title!r})" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from typing import TYPE_CHECKING, Annotated, Optional | ||
|
||
from annotated_types import MaxLen, MinLen | ||
from pydantic import ConfigDict | ||
|
||
from fastapi_jsonapi.schema_base import BaseModel | ||
from fastapi_jsonapi.types_metadata import RelationshipInfo | ||
|
||
name_constrained = Annotated[ | ||
str, | ||
MinLen(1), | ||
MaxLen(20), | ||
] | ||
|
||
if TYPE_CHECKING: | ||
from examples.api_for_sqlalchemy.schemas.movie import MovieSchema | ||
|
||
|
||
class AgeRatingAttributesSchema(BaseModel): | ||
model_config = ConfigDict( | ||
from_attributes=True, | ||
) | ||
name: str | ||
description: str | ||
|
||
|
||
class AgeRatingBaseSchema(AgeRatingAttributesSchema): | ||
movies: Annotated[ | ||
Optional[list["MovieSchema"]], | ||
RelationshipInfo( | ||
resource_type="movie", | ||
many=True, | ||
), | ||
] = None | ||
|
||
|
||
class AgeRatingCreateSchema(AgeRatingBaseSchema): | ||
name: name_constrained | ||
|
||
|
||
class AgeRatingUpdateSchema(AgeRatingBaseSchema): | ||
name: Optional[name_constrained] = None | ||
description: Optional[str] = None | ||
|
||
|
||
class AgeRatingSchema(AgeRatingBaseSchema): | ||
""" | ||
Age Rating | ||
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from datetime import date | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Annotated, | ||
Optional, | ||
) | ||
|
||
from annotated_types import MaxLen, MinLen | ||
from pydantic import ConfigDict | ||
|
||
from fastapi_jsonapi.schema_base import BaseModel | ||
from fastapi_jsonapi.types_metadata import RelationshipInfo | ||
|
||
if TYPE_CHECKING: | ||
from examples.api_for_sqlalchemy.schemas.age_rating import AgeRatingSchema | ||
|
||
title_constrained = Annotated[ | ||
str, | ||
MinLen(1), | ||
MaxLen(120), | ||
] | ||
|
||
|
||
class MovieAttributesSchema(BaseModel): | ||
model_config = ConfigDict( | ||
from_attributes=True, | ||
) | ||
title: str | ||
description: str | ||
age_rating: Optional[str] = None | ||
|
||
|
||
class MovieBaseSchema(MovieAttributesSchema): | ||
age_rating_obj: Annotated[ | ||
Optional["AgeRatingSchema"], | ||
RelationshipInfo( | ||
resource_type="age-rating", | ||
resource_id_example="PG-13", | ||
id_field_name="name", | ||
), | ||
] = None | ||
|
||
|
||
class MovieCreateSchema(MovieBaseSchema): | ||
""" | ||
Create | ||
""" | ||
|
||
title: title_constrained | ||
|
||
|
||
class MovieUpdateSchema(MovieBaseSchema): | ||
title: Optional[title_constrained] = None | ||
description: Optional[str] = None | ||
release_date: Optional[date] = None | ||
duration: Optional[int] = None | ||
|
||
|
||
class MovieSchema(MovieBaseSchema): | ||
id: int |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Зачем?