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] AttributeError: 'method' object has no attribute '_ninja_operation' #1375

Closed
shrpow opened this issue Dec 30, 2024 · 2 comments
Closed

Comments

@shrpow
Copy link

shrpow commented Dec 30, 2024

Describe the bug
When I'm trying to create a class-based router using the ApiRouter class as a base class, I receive this error at the time of self.add_api_operations:

    view_func._ninja_operation = operation  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'method' object has no attribute '_ninja_operation'

When I comment out this line in the source code for django-ninja my project works absolutely correctly.

Code snippet:

class TestRouter(Router):
    def __init__(self: Self) -> None:
        super().__init__()

        self.tags = ["Router"]

        self.add_api_operation(
            methods=["POST"],
            path="/asd",
            view_func=self.hello,
        )

    def hello(self: Self, request: WSGIRequest) -> str:
        return "ok"

Versions (please complete the following information):

  • Python version: 3.12.3

Note you can quickly get this by runninng in ./manage.py shell this line:

>>> import django; import pydantic; import ninja; django.__version__; ninja.__version__; pydantic.__version__
'5.1.3'
'1.3.0'
'2.10.4'
@vitalik
Copy link
Owner

vitalik commented Dec 30, 2024

@shrpow well methods in python work a bit different than module functions

try the following:

class TestRouter(Router):
    def __init__(self: Self) -> None:
        super().__init__()

        self.tags = ["Router"]


        def hello(request: WSGIRequest) -> str:
            # you can access self her
            return "ok"

        self.add_api_operation(
            methods=["POST"],
            path="/asd",
            view_func=hello,
        )


``

@shrpow
Copy link
Author

shrpow commented Jan 3, 2025

it works! thx

@shrpow shrpow closed this as completed Jan 3, 2025
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