Open tmorgan497 opened 1 day ago
@tmorgan497 I will look into this. But at the moment can you share your python
version, ninja-extra
version and your pydantic
version
Also, the error message does not show where you registered the controller
I copied your code to a new Django project and it worked perfectly
@tmorgan497 I will look into this. But at the moment can you share your
python
version,ninja-extra
version and yourpydantic
version
My versions are:
I purged my docker volumes and reinstalled and now I'm not getting the import error. I also switched to using the router that I already have in this module. However, I now have an issue where I have to pass self
as a parameter to the endpoint. Is there a way to suppress self
as a query parameter for the endpoint? If I remove self
from the args, then I get a linting error (which I can suppress, but I didn't know if this is the proper way to go about this).
"""API endpoints for the shop app."""
# from __future__ import annotations
import logging
from shop.schemas import BaseQueryParams, ProductResponse, ProductReadSchema, ProductCreateSchema # isort: skip
from ninja import Query, Router
from django.http import HttpRequest
from ninja_extra import ControllerBase, permissions, api_controller
from django.db.models import Q
from django.core.paginator import Paginator
from ninja_jwt.authentication import JWTAuth
from django.contrib.postgres.search import TrigramSimilarity
from api.api import GenericResponse
from shop.models import Vendor, Product, ProductImage
router = Router(tags=["Shop"])
logger = logging.getLogger(__name__)
class IsCmsAdmin(permissions.BasePermission):
"""Custom permission for cms_admin role."""
def has_permission(self, request: HttpRequest) -> bool:
"""Check if user has cms_admin role."""
user = request.auth
return user and user.groups.filter(name="cms_admin").exists()
@api_controller("shop_class/", auth=[JWTAuth()], permissions=[IsCmsAdmin])
class ProductController(ControllerBase):
"""Product API controller."""
@router.post("/product")
def create_product(
request: HttpRequest,
payload: ProductCreateSchema,
) -> dict:
"""Create product API endpoint."""
product = Product.objects.create(
name=payload.name,
description=payload.description,
price=payload.price,
)
return {
"id": product.id,
"name": product.name,
"description": product.description,
"price": product.price,
}
Hello. I'm having an issue defining a controller for a POST endpoint containing a body with a ninja schema. It seems like ninja_extra is importing the ProductCreateSchema after it creates the api_controller causing it to not be able to import ProductCreateSchema. I also attempted importing my schema before importing ninja_extra, but no luck.