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

[BUG] Order of operations affect response and request schemas #1104

Open
dc-p8 opened this issue Mar 10, 2024 · 1 comment
Open

[BUG] Order of operations affect response and request schemas #1104

dc-p8 opened this issue Mar 10, 2024 · 1 comment

Comments

@dc-p8
Copy link

dc-p8 commented Mar 10, 2024

  • Python version: 3.12.2
  • Django version: 4.2.0
  • Django-Ninja version: 1.1.0
  • Pydantic version: 2.5.3

this code

class Author(models.Model):
    name =models.CharField(max_length=255)

class Book(models.Model):
    name=models.CharField(max_length=255)
    author = models.ForeignKey(Author, on_delete=models.CASCADE, null=True, db_index=False)



class BookSchema(ModelSchema):
    class Meta:
        model = Book
        fields = '__all__'

@router.put("/books/{id}/", response=BookSchema)
def put_book(request, pk: int, data: BookSchema):
    return Book.objects.get(id=id)


@router.get("/books/{id}/", response=BookSchema)
def get_book_detail(request, id: int):
    return Book.objects.get(id=id)

gives this schemas
image

just change the order of routes

class Author(models.Model):
    name =models.CharField(max_length=255)

class Book(models.Model):
    name=models.CharField(max_length=255)
    author = models.ForeignKey(Author, on_delete=models.CASCADE, null=True, db_index=False)



class BookSchema(ModelSchema):
    class Meta:
        model = Book
        fields = '__all__'
        
        
@router.get("/books/{id}/", response=BookSchema)
def get_book_detail(request, id: int):
    return Book.objects.get(id=id)


@router.put("/books/{id}/", response=BookSchema)
def put_book(request, pk: int, data: BookSchema):
    return Book.objects.get(id=id)

and you get a different schema

image
@dc-p8
Copy link
Author

dc-p8 commented Mar 11, 2024

when having only a post route

@router.put("/books/{id}/", response=create_schema(Book))
def put_book(request, pk: int, data: BookSchema):
    return Book.objects.get(id=id)    

doest produce the same response schema as

@router.put("/books/{id}/", response=BookSchema)
def put_book(request, pk: int, data: BookSchema):
    return Book.objects.get(id=id)

The inputschema and the response shema should contain author, not author_id.

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

1 participant