Skip to content
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

Field with list factory causes API docs to not load with non-serializable. #1116

Open
wachpwnski opened this issue Mar 29, 2024 · 2 comments

Comments

@wachpwnski
Copy link

When I use Field(list, alias="model related name") API doc generation no longer works and fails out with an issue with serializing. I'm not sure if this is how I am doing it or if I should make it Optional[List[BookSchema]] in the case that no matching foreign keys exist.

class Author(models.Model):
    first_name = models.CharField(max_length=64)
    last_name = models.CharField(max_length=64)

class Book(models.Model):
    title = models.CharField(max_length=64)
    author = models.ForeignKey(
        Author,
        related_name="books",
        null=False,
        on_delete=models.CASCADE,
    )

class BookSchema(Schema):
    title: str

class AuthorSchema(Schema):
    first_name: str
    last_name: str
    books: List[BookSchema] = Field(list, alias="books")  # Causes an error in API docs
@vitalik
Copy link
Owner

vitalik commented Mar 29, 2024

@wachpwnski

try:

class AuthorSchema(Schema):
    ...
    books: List[BookSchema] = Field([], alias="books") 

@wachpwnski
Copy link
Author

@wachpwnski

try:

class AuthorSchema(Schema):
    ...
    books: List[BookSchema] = Field([], alias="books") 

This fixed my issue. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants