python - I am attempting to use Django Ninja for the first time and running into a strange error - Stack Overflow

admin2025-04-17  2

I cant quite understand error I am receiving. I am simply trying to setup a model schema for my model. I am an old Django hand but ninja is new for me. What am I doing wrong here? Would love some help and feedback.

My model is this

class Program(models.Model):
    mentor = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    description = models.TextField()
    start_date = models.DateField()
    end_date = models.DateField()
    attendees = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="attendees")
    participants = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="participants")

    created_on = models.DateTimeField(auto_now_add = True)
    updated_on = models.DateTimeField(auto_now = True)

My api.py has the following definition

class MentorOutSchema(ModelSchema):
    class Meta:
        model = Program
        fields = [
                "mentor",
                "description",
                "start_date",
                "end_date",
                "attendees",
                "participants",
            ]

My endpoint is this

@router.get('/programs')
async def mentor_programs(request, response=list[MentorOutSchema]):
    return Program.objects.filter(mentor=request.user)

When I start the server, I get the following error

    @router.get('/programs')
     ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/ninja/router.py", line 268, in decorator
    self.add_api_operation(
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/ninja/router.py", line 319, in add_api_operation
    path_view.add_operation(
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/ninja/operation.py", line 426, in add_operation
    operation = OperationClass(
                ^^^^^^^^^^^^^^^
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/ninja/operation.py", line 331, in __init__
    super().__init__(*args, **kwargs)
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/ninja/operation.py", line 82, in __init__
    self.signature = ViewSignature(self.path, self.view_func)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/ninja/signature/details.py", line 87, in __init__
    self.models: TModels = self._create_models()
                           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/ninja/signature/details.py", line 171, in _create_models
    model_cls = type(cls_name, (base_cls,), attrs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 219, in __new__
    set_model_fields(cls, bases, config_wrapper, ns_resolver)
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 537, in set_model_fields
    fields, class_vars = collect_model_fields(cls, bases, config_wrapper, ns_resolver, typevars_map=typevars_map)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/pydantic/_internal/_fields.py", line 220, in collect_model_fields
    _warn_on_nested_alias_in_annotation(ann_type, ann_name)
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/pydantic/_internal/_fields.py", line 258, in _warn_on_nested_alias_in_annotation
    for anno_arg in args:
                    ^^^^
TypeError: 'member_descriptor' object is not iterable

I cant quite understand error I am receiving. I am simply trying to setup a model schema for my model. I am an old Django hand but ninja is new for me. What am I doing wrong here? Would love some help and feedback.

My model is this

class Program(models.Model):
    mentor = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    description = models.TextField()
    start_date = models.DateField()
    end_date = models.DateField()
    attendees = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="attendees")
    participants = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="participants")

    created_on = models.DateTimeField(auto_now_add = True)
    updated_on = models.DateTimeField(auto_now = True)

My api.py has the following definition

class MentorOutSchema(ModelSchema):
    class Meta:
        model = Program
        fields = [
                "mentor",
                "description",
                "start_date",
                "end_date",
                "attendees",
                "participants",
            ]

My endpoint is this

@router.get('/programs')
async def mentor_programs(request, response=list[MentorOutSchema]):
    return Program.objects.filter(mentor=request.user)

When I start the server, I get the following error

    @router.get('/programs')
     ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/ninja/router.py", line 268, in decorator
    self.add_api_operation(
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/ninja/router.py", line 319, in add_api_operation
    path_view.add_operation(
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/ninja/operation.py", line 426, in add_operation
    operation = OperationClass(
                ^^^^^^^^^^^^^^^
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/ninja/operation.py", line 331, in __init__
    super().__init__(*args, **kwargs)
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/ninja/operation.py", line 82, in __init__
    self.signature = ViewSignature(self.path, self.view_func)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/ninja/signature/details.py", line 87, in __init__
    self.models: TModels = self._create_models()
                           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/ninja/signature/details.py", line 171, in _create_models
    model_cls = type(cls_name, (base_cls,), attrs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 219, in __new__
    set_model_fields(cls, bases, config_wrapper, ns_resolver)
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 537, in set_model_fields
    fields, class_vars = collect_model_fields(cls, bases, config_wrapper, ns_resolver, typevars_map=typevars_map)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/pydantic/_internal/_fields.py", line 220, in collect_model_fields
    _warn_on_nested_alias_in_annotation(ann_type, ann_name)
  File "/home/vivekv/.cache/pypoetry/virtualenvs/pifnow-Zp6OiFzb-py3.12/lib/python3.12/site-packages/pydantic/_internal/_fields.py", line 258, in _warn_on_nested_alias_in_annotation
    for anno_arg in args:
                    ^^^^
TypeError: 'member_descriptor' object is not iterable
Share edited Jan 31 at 9:31 vivekv asked Jan 31 at 2:16 vivekvvivekv 2,2984 gold badges23 silver badges38 bronze badges 4
  • in schema class should be class Meta not class Config – sahasrara62 Commented Jan 31 at 6:54
  • @sahasrara62 thanks for pointing it out. Changed it but same problem persists. – vivekv Commented Jan 31 at 9:30
  • 1 django-ninja cannot parse your controller signature, it is response=list[MentorOutSchema], should be moved here ⇾ @router.get('/programs', response=list[MentorOutSchema]) – Serhii Fomenko Commented Jan 31 at 9:40
  • 1 @vivekv, you're welcome. You are likely to have another problem, you can't just return queryset as it is: Program.objects.filter(mentor=request.user), django, will swear SynchronousOnlyOperation, from the fact that your controller is async, and the request will execute synchronously eventually, but something like this will work ⇾ [v async for v in your_queryset]. – Serhii Fomenko Commented Jan 31 at 10:11
Add a comment  | 

1 Answer 1

Reset to default 0

As explained by @SerhiiFomenko the issue was in my api.py endpoint method signature

@router.get('/programs', response=list[MentorOutSchema])
 def mentor_programs(request):
    return Program.objects.filter(mentor=request.user)
转载请注明原文地址:http://anycun.com/QandA/1744883076a88970.html