mirror of
https://github.com/inventree/InvenTree.git
synced 2025-12-16 09:18:10 +00:00
Fix for parametric part table
- Only display parameters for which we know there is a value
This commit is contained in:
@@ -27,6 +27,7 @@ from rest_framework.exceptions import NotAcceptable, NotFound, PermissionDenied
|
|||||||
from rest_framework.permissions import IsAdminUser, IsAuthenticated
|
from rest_framework.permissions import IsAdminUser, IsAuthenticated
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
from sql_util.utils import SubqueryCount
|
||||||
|
|
||||||
import common.filters
|
import common.filters
|
||||||
import common.models
|
import common.models
|
||||||
@@ -820,6 +821,27 @@ class ParameterTemplateFilter(FilterSet):
|
|||||||
queryset, 'model_type', value, allow_null=True
|
queryset, 'model_type', value, allow_null=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
exists_for_model = rest_filters.CharFilter(
|
||||||
|
method='filter_exists_for_model', label='Exists For Model'
|
||||||
|
)
|
||||||
|
|
||||||
|
def filter_exists_for_model(self, queryset, name, value):
|
||||||
|
"""Filter queryset to include only ParameterTemplates which have at least one Parameter for the given model type."""
|
||||||
|
content_type = common.filters.determine_content_type(value)
|
||||||
|
|
||||||
|
if not content_type:
|
||||||
|
return queryset.none()
|
||||||
|
|
||||||
|
# Annotate the queryset to determine which ParameterTemplates have at least one Parameter for the given model type
|
||||||
|
queryset = queryset.annotate(
|
||||||
|
parameter_count=SubqueryCount(
|
||||||
|
'parameters', filter=Q(model_type=content_type)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Return only those ParameterTemplates which have at least one Parameter for the given model type
|
||||||
|
return queryset.filter(parameter_count__gt=0)
|
||||||
|
|
||||||
|
|
||||||
class ParameterTemplateMixin:
|
class ParameterTemplateMixin:
|
||||||
"""Mixin class for ParameterTemplate views."""
|
"""Mixin class for ParameterTemplate views."""
|
||||||
|
|||||||
@@ -21,22 +21,17 @@ import InvenTree.conversion
|
|||||||
import InvenTree.helpers
|
import InvenTree.helpers
|
||||||
|
|
||||||
|
|
||||||
def filter_content_type(
|
def determine_content_type(content_type: str | int | None) -> ContentType | None:
|
||||||
queryset, field_name: str, content_type: str | int | None, allow_null: bool = True
|
"""Determine a ContentType instance from a string or integer input.
|
||||||
):
|
|
||||||
"""Filter a queryset by content type.
|
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
queryset: The queryset to filter.
|
content_type: The content type to resolve (name or ID).
|
||||||
field_name: The name of the content type field within the current model context.
|
|
||||||
content_type: The content type to filter by (name or ID).
|
|
||||||
allow_null: If True, include entries with null content type.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Filtered queryset.
|
ContentType instance if found, else None.
|
||||||
"""
|
"""
|
||||||
if content_type is None:
|
if content_type is None:
|
||||||
return queryset
|
return None
|
||||||
|
|
||||||
ct = None
|
ct = None
|
||||||
|
|
||||||
@@ -59,6 +54,28 @@ def filter_content_type(
|
|||||||
# Next, try to resolve the content type via a model name
|
# Next, try to resolve the content type via a model name
|
||||||
ct = ContentType.objects.filter(model__iexact=content_type).first()
|
ct = ContentType.objects.filter(model__iexact=content_type).first()
|
||||||
|
|
||||||
|
return ct
|
||||||
|
|
||||||
|
|
||||||
|
def filter_content_type(
|
||||||
|
queryset, field_name: str, content_type: str | int | None, allow_null: bool = True
|
||||||
|
):
|
||||||
|
"""Filter a queryset by content type.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
queryset: The queryset to filter.
|
||||||
|
field_name: The name of the content type field within the current model context.
|
||||||
|
content_type: The content type to filter by (name or ID).
|
||||||
|
allow_null: If True, include entries with null content type.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Filtered queryset.
|
||||||
|
"""
|
||||||
|
if content_type is None:
|
||||||
|
return queryset
|
||||||
|
|
||||||
|
ct = determine_content_type(content_type)
|
||||||
|
|
||||||
if ct is None:
|
if ct is None:
|
||||||
raise ValueError(f'Invalid content type: {content_type}')
|
raise ValueError(f'Invalid content type: {content_type}')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user