Skip to content

Commit

Permalink
feat: support property decorated method fields in the list endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jhassine committed Nov 29, 2024
1 parent 8fc5eb2 commit 65fe9fc
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion django_ninja_crudl/crudl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""CRUDL API base class."""

import logging
from enum import Enum, IntEnum
from typing import TYPE_CHECKING, Any, ClassVar, Literal, cast
from uuid import UUID
Expand Down Expand Up @@ -54,6 +55,8 @@
if TYPE_CHECKING:
from django.db.models.manager import BaseManager

logger = logging.getLogger("django_ninja_crudl")


class CrudlApiBaseMeta:
"""Base meta class for the CRUDL API configuration."""
Expand Down Expand Up @@ -709,7 +712,24 @@ def get_many(
related_models: list[str] = []
many_to_many_models: list[str] = []
related_fields: list[str] = []
property_fields: list[str] = []

for field_name, field in ListSchema.model_fields.items():
attr = getattr(model_class, field_name, None)

# Skip @property methods here
if attr and isinstance(attr, property):
property_fields.append(field_name)
# TODO: Implement a way to avoid N+1 queries when using @property methods
logger.debug(
"Detected use of @property method %s of the model %s in"
" list_fields definition which"
" may cause N+1 queries and cause performance degradation.",
field_name,
model_class,
)
return qs

django_field = model_class._meta.get_field(field_name) # noqa: SLF001
if isinstance(
django_field,
Expand Down Expand Up @@ -739,7 +759,8 @@ def get_many(
non_related_fields: list[str] = [
field_name
for field_name in ListSchema.model_fields.keys()
if field_name not in related_models + many_to_many_models
if field_name
not in related_models + many_to_many_models + property_fields
]

all_fields: list[str] = non_related_fields + related_fields
Expand Down

0 comments on commit 65fe9fc

Please sign in to comment.