mirror of
https://github.com/inventree/InvenTree.git
synced 2026-11-23 00:06:16 +00:00
More efficient thumbnail field
This commit is contained in:
1 parent
9e5e9209a5
commit
8911c22e35
3 files changed
+24
-3
No files matched your search
@@ -944,6 +944,26 @@ class InvenTreeAttachmentSerializerField(serializers.FileField):
|
|||||||
return default_storage.url(str(value))
|
return default_storage.url(str(value))
|
||||||
|
|
||||||
|
|
||||||
|
class InvenTreeThumbnailField(serializers.CharField):
|
||||||
|
"""Read-only field which renders the thumbnail URL for a model instance.
|
||||||
|
|
||||||
|
The instance must implement get_thumbnail_url() (e.g. InvenTreeImageMixin).
|
||||||
|
|
||||||
|
Note: This is more efficient than CharField(source='get_thumbnail_url'),
|
||||||
|
as DRF inspects the signature of a callable source for every serialized row.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
"""Initialize the field as read-only, sourced from the instance itself."""
|
||||||
|
kwargs['source'] = '*'
|
||||||
|
kwargs['read_only'] = True
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
|
def to_representation(self, value):
|
||||||
|
"""Return the thumbnail URL for the provided instance."""
|
||||||
|
return value.get_thumbnail_url()
|
||||||
|
|
||||||
|
|
||||||
class InvenTreeImageSerializerField(serializers.ImageField):
|
class InvenTreeImageSerializerField(serializers.ImageField):
|
||||||
"""Custom image serializer.
|
"""Custom image serializer.
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ from InvenTree.serializers import (
|
|||||||
InvenTreeModelSerializer,
|
InvenTreeModelSerializer,
|
||||||
InvenTreeMoneySerializer,
|
InvenTreeMoneySerializer,
|
||||||
InvenTreeTaggitSerializer,
|
InvenTreeTaggitSerializer,
|
||||||
|
InvenTreeThumbnailField,
|
||||||
OptionalField,
|
OptionalField,
|
||||||
apply_duplicate_copy_options,
|
apply_duplicate_copy_options,
|
||||||
)
|
)
|
||||||
@@ -58,7 +59,7 @@ class CompanyBriefSerializer(InvenTreeModelSerializer):
|
|||||||
|
|
||||||
image = InvenTreeImageSerializerField(read_only=True)
|
image = InvenTreeImageSerializerField(read_only=True)
|
||||||
|
|
||||||
thumbnail = serializers.CharField(source='get_thumbnail_url', read_only=True)
|
thumbnail = InvenTreeThumbnailField()
|
||||||
|
|
||||||
|
|
||||||
@register_importer()
|
@register_importer()
|
||||||
|
|||||||
@@ -323,7 +323,7 @@ class PartBriefSerializer(
|
|||||||
image = InvenTree.serializers.InvenTreeImageSerializerField(
|
image = InvenTree.serializers.InvenTreeImageSerializerField(
|
||||||
read_only=True, allow_null=True
|
read_only=True, allow_null=True
|
||||||
)
|
)
|
||||||
thumbnail = serializers.CharField(source='get_thumbnail_url', read_only=True)
|
thumbnail = InvenTree.serializers.InvenTreeThumbnailField()
|
||||||
|
|
||||||
IPN = serializers.CharField(
|
IPN = serializers.CharField(
|
||||||
required=False,
|
required=False,
|
||||||
@@ -840,7 +840,7 @@ class PartSerializer(
|
|||||||
image = InvenTree.serializers.InvenTreeImageSerializerField(
|
image = InvenTree.serializers.InvenTreeImageSerializerField(
|
||||||
required=False, allow_null=True
|
required=False, allow_null=True
|
||||||
)
|
)
|
||||||
thumbnail = serializers.CharField(source='get_thumbnail_url', read_only=True)
|
thumbnail = InvenTree.serializers.InvenTreeThumbnailField()
|
||||||
starred = serializers.SerializerMethodField()
|
starred = serializers.SerializerMethodField()
|
||||||
|
|
||||||
# PrimaryKeyRelated fields (Note: enforcing field type here results in much faster queries, somehow...)
|
# PrimaryKeyRelated fields (Note: enforcing field type here results in much faster queries, somehow...)
|
||||||
|
|||||||
Reference in new issue
Block a user