drf-react-by-schema 0.28.0__tar.gz → 0.28.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/PKG-INFO +34 -1
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/README.rst +33 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/metadata.py +2 -1
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/serializers.py +90 -10
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/utils.py +11 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/viewsets.py +43 -38
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema.egg-info/PKG-INFO +34 -1
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/setup.cfg +1 -1
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/LICENSE +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/MANIFEST.in +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/__init__.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/admin.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/apps.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/auth.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/autocomplete_serializers.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/autocomplete_viewsets.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/checks.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/date_utils.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/__init__.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/array.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/boolean.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/date.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/file.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/geometry.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/number.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/relational.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/serializers.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/text.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/filters.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/management/__init__.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/management/commands/__init__.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/management/commands/sync_react.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/migrations/__init__.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/mixins.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/models.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/pagination.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/patches/__init__.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/patches/drf_serializers.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/renderers.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/schema_bundle.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/tests.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/typescript_generator.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/urls.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/utils_cache.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/views.py +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema.egg-info/SOURCES.txt +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema.egg-info/dependency_links.txt +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema.egg-info/requires.txt +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema.egg-info/top_level.txt +0 -0
- {drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/setup.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: drf_react_by_schema
|
|
3
|
-
Version: 0.28.
|
|
3
|
+
Version: 0.28.1
|
|
4
4
|
Summary: A Django app to easily integrate with React from models.
|
|
5
5
|
Home-page: https://gitlab.com/eita/drf-react-by-schema/drf-react-by-schema-django
|
|
6
6
|
Author: Cooperativa EITA
|
|
@@ -252,6 +252,39 @@ Quick start
|
|
|
252
252
|
|
|
253
253
|
You can customize ``viewsets`` and ``serializers`` to annotate other attributes.
|
|
254
254
|
|
|
255
|
+
List vs. detail serializers
|
|
256
|
+
---------------------------
|
|
257
|
+
The generated ``XSerializer`` is the canonical serializer and serves both ``retrieve``
|
|
258
|
+
and ``list``/``excel``. When a detail-only field would be expensive in lists, declare it
|
|
259
|
+
in ``Meta.list_exclude`` on the app's general serializer:
|
|
260
|
+
|
|
261
|
+
.. code-block:: python
|
|
262
|
+
|
|
263
|
+
from drf_react_by_schema.serializers import (
|
|
264
|
+
ClienteSerializer as BaseClienteSerializer,
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
class ClienteSerializer(BaseClienteSerializer):
|
|
268
|
+
contratacao = drf_serializers.SerializerMethodField() # detail only
|
|
269
|
+
nome = drf_serializers.CharField(read_only=True) # both
|
|
270
|
+
|
|
271
|
+
class Meta(BaseClienteSerializer.Meta):
|
|
272
|
+
list_exclude = ("contratacao",)
|
|
273
|
+
|
|
274
|
+
The package derives (and caches) a list projection that keeps every other field,
|
|
275
|
+
including future additions to the general serializer. ``list_exclude`` takes serializer
|
|
276
|
+
field names, not model ``source`` paths; ``()`` means "the list is identical to the
|
|
277
|
+
detail response". An explicit list serializer remains available as an escape hatch:
|
|
278
|
+
|
|
279
|
+
.. code-block:: python
|
|
280
|
+
|
|
281
|
+
class ClienteViewSet(GenericViewSet):
|
|
282
|
+
serializer_class = ClienteSerializer
|
|
283
|
+
serializer_list_class = ClienteListSerializer # optional, full override
|
|
284
|
+
|
|
285
|
+
``XListSerializer`` classes are no longer generated automatically: the default is the
|
|
286
|
+
general serializer, so app-added fields can never silently disappear from lists.
|
|
287
|
+
|
|
255
288
|
Global permission-aware queryset filtering
|
|
256
289
|
------------------------------------------
|
|
257
290
|
Every generated viewset already supports a ``filter_by_callback`` (and a list-only
|
|
@@ -220,6 +220,39 @@ Quick start
|
|
|
220
220
|
|
|
221
221
|
You can customize ``viewsets`` and ``serializers`` to annotate other attributes.
|
|
222
222
|
|
|
223
|
+
List vs. detail serializers
|
|
224
|
+
---------------------------
|
|
225
|
+
The generated ``XSerializer`` is the canonical serializer and serves both ``retrieve``
|
|
226
|
+
and ``list``/``excel``. When a detail-only field would be expensive in lists, declare it
|
|
227
|
+
in ``Meta.list_exclude`` on the app's general serializer:
|
|
228
|
+
|
|
229
|
+
.. code-block:: python
|
|
230
|
+
|
|
231
|
+
from drf_react_by_schema.serializers import (
|
|
232
|
+
ClienteSerializer as BaseClienteSerializer,
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
class ClienteSerializer(BaseClienteSerializer):
|
|
236
|
+
contratacao = drf_serializers.SerializerMethodField() # detail only
|
|
237
|
+
nome = drf_serializers.CharField(read_only=True) # both
|
|
238
|
+
|
|
239
|
+
class Meta(BaseClienteSerializer.Meta):
|
|
240
|
+
list_exclude = ("contratacao",)
|
|
241
|
+
|
|
242
|
+
The package derives (and caches) a list projection that keeps every other field,
|
|
243
|
+
including future additions to the general serializer. ``list_exclude`` takes serializer
|
|
244
|
+
field names, not model ``source`` paths; ``()`` means "the list is identical to the
|
|
245
|
+
detail response". An explicit list serializer remains available as an escape hatch:
|
|
246
|
+
|
|
247
|
+
.. code-block:: python
|
|
248
|
+
|
|
249
|
+
class ClienteViewSet(GenericViewSet):
|
|
250
|
+
serializer_class = ClienteSerializer
|
|
251
|
+
serializer_list_class = ClienteListSerializer # optional, full override
|
|
252
|
+
|
|
253
|
+
``XListSerializer`` classes are no longer generated automatically: the default is the
|
|
254
|
+
general serializer, so app-added fields can never silently disappear from lists.
|
|
255
|
+
|
|
223
256
|
Global permission-aware queryset filtering
|
|
224
257
|
------------------------------------------
|
|
225
258
|
Every generated viewset already supports a ``filter_by_callback`` (and a list-only
|
|
@@ -15,7 +15,8 @@ class _PinnedSerializerClass:
|
|
|
15
15
|
|
|
16
16
|
``SimpleMetadata.determine_metadata`` derives the POST fields from
|
|
17
17
|
``view.get_serializer()``, which is action-dependent (``list``/``excel``
|
|
18
|
-
use
|
|
18
|
+
may use an explicit list serializer or a ``Meta.list_exclude`` projection).
|
|
19
|
+
Pinning keeps the schema canonical.
|
|
19
20
|
"""
|
|
20
21
|
|
|
21
22
|
def __init__(self, view, serializer_class):
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/serializers.py
RENAMED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import traceback
|
|
2
|
+
from functools import lru_cache
|
|
3
|
+
|
|
2
4
|
from django.apps import apps
|
|
3
5
|
from django.contrib.auth import get_user_model
|
|
4
6
|
from django.contrib.auth import password_validation
|
|
5
7
|
from django.contrib.auth.tokens import default_token_generator
|
|
8
|
+
from django.core.exceptions import ImproperlyConfigured
|
|
6
9
|
from django.core.exceptions import ValidationError as DjangoValidationError
|
|
7
10
|
from django.utils.encoding import force_str
|
|
8
11
|
from django.utils.http import urlsafe_base64_decode
|
|
@@ -148,6 +151,11 @@ class GenericSerializerMetaclass(SerializerMetaclass):
|
|
|
148
151
|
# First, let DRF's SerializerMetaclass do its work
|
|
149
152
|
new_class = super().__new__(mcs, name, bases, attrs)
|
|
150
153
|
|
|
154
|
+
# List projections inherit the detail serializer's related-field setup;
|
|
155
|
+
# re-running it would rebuild update_related/model_fields unnecessarily.
|
|
156
|
+
if attrs.get("_drbs_projection"):
|
|
157
|
+
return new_class
|
|
158
|
+
|
|
151
159
|
# Only process if this class has a Meta with a model
|
|
152
160
|
if hasattr(new_class, "Meta") and hasattr(new_class.Meta, "model"):
|
|
153
161
|
mcs._populate_related_fields(new_class)
|
|
@@ -494,6 +502,88 @@ class GenericSerializer(
|
|
|
494
502
|
fields = "__all__"
|
|
495
503
|
|
|
496
504
|
|
|
505
|
+
class _ListProjectionMixin:
|
|
506
|
+
"""Drop the detail-only fields from a projected list serializer."""
|
|
507
|
+
|
|
508
|
+
def get_fields(self):
|
|
509
|
+
fields = super().get_fields()
|
|
510
|
+
for name in getattr(self.Meta, "list_exclude", ()) or ():
|
|
511
|
+
fields.pop(name, None)
|
|
512
|
+
return fields
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
def _known_serializer_field_names(detail_class):
|
|
516
|
+
"""Field names that ``Meta.list_exclude`` may legitimately reference."""
|
|
517
|
+
known = set(getattr(detail_class, "_declared_fields", {}) or {})
|
|
518
|
+
meta = getattr(detail_class, "Meta", None)
|
|
519
|
+
model = getattr(meta, "model", None)
|
|
520
|
+
if model is not None:
|
|
521
|
+
known.update(field.name for field in model._meta.get_fields())
|
|
522
|
+
meta_fields = getattr(meta, "fields", None)
|
|
523
|
+
if isinstance(meta_fields, (list, tuple)):
|
|
524
|
+
known.update(meta_fields)
|
|
525
|
+
elif isinstance(meta_fields, dict):
|
|
526
|
+
known.update(meta_fields)
|
|
527
|
+
return known
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
def _list_serializer_class_name(detail_class):
|
|
531
|
+
suffix = "Serializer"
|
|
532
|
+
name = detail_class.__name__
|
|
533
|
+
base = name[: -len(suffix)] if name.endswith(suffix) else name
|
|
534
|
+
return f"{base}ListSerializer"
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
@lru_cache(maxsize=None)
|
|
538
|
+
def _build_list_projection(detail_class, excludes):
|
|
539
|
+
meta = type("Meta", (detail_class.Meta,), {"list_exclude": excludes})
|
|
540
|
+
return type(
|
|
541
|
+
_list_serializer_class_name(detail_class),
|
|
542
|
+
(_ListProjectionMixin, detail_class),
|
|
543
|
+
{
|
|
544
|
+
"Meta": meta,
|
|
545
|
+
"__module__": detail_class.__module__,
|
|
546
|
+
"_drbs_projection": True,
|
|
547
|
+
},
|
|
548
|
+
)
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
def list_serializer_for(detail_class):
|
|
552
|
+
"""List serializer derived from ``detail_class``, or ``None``.
|
|
553
|
+
|
|
554
|
+
The general serializer is the canonical shape and is used for list/excel by
|
|
555
|
+
default. Setting ``Meta.list_exclude`` opts into a lighter list that keeps
|
|
556
|
+
every other field (including app-added ones) and drops the listed names.
|
|
557
|
+
"""
|
|
558
|
+
if detail_class is None:
|
|
559
|
+
return None
|
|
560
|
+
meta = getattr(detail_class, "Meta", None)
|
|
561
|
+
excludes = getattr(meta, "list_exclude", None)
|
|
562
|
+
if excludes is None:
|
|
563
|
+
return None
|
|
564
|
+
if isinstance(excludes, str) or not isinstance(
|
|
565
|
+
excludes, (list, tuple, set, frozenset)
|
|
566
|
+
):
|
|
567
|
+
raise ImproperlyConfigured(
|
|
568
|
+
f"{detail_class.__name__}.Meta.list_exclude must be a list or tuple "
|
|
569
|
+
"of serializer field names."
|
|
570
|
+
)
|
|
571
|
+
if isinstance(excludes, (set, frozenset)):
|
|
572
|
+
excludes = tuple(sorted(excludes))
|
|
573
|
+
else:
|
|
574
|
+
excludes = tuple(excludes)
|
|
575
|
+
if not excludes:
|
|
576
|
+
return None
|
|
577
|
+
|
|
578
|
+
unknown = set(excludes) - _known_serializer_field_names(detail_class)
|
|
579
|
+
if unknown:
|
|
580
|
+
raise ImproperlyConfigured(
|
|
581
|
+
f"{detail_class.__name__}.Meta.list_exclude references unknown "
|
|
582
|
+
f"field(s): {', '.join(sorted(unknown))}."
|
|
583
|
+
)
|
|
584
|
+
return _build_list_projection(detail_class, excludes)
|
|
585
|
+
|
|
586
|
+
|
|
497
587
|
######################
|
|
498
588
|
|
|
499
589
|
|
|
@@ -501,19 +591,9 @@ for app in settings["APPS"]:
|
|
|
501
591
|
for name, model in apps.all_models[app].items():
|
|
502
592
|
model_name = model.__name__
|
|
503
593
|
if isinstance(model, type):
|
|
504
|
-
serializer_list_name = f"{model_name}ListSerializer"
|
|
505
594
|
serializer_name = f"{model_name}Serializer"
|
|
506
595
|
if not serializer_name in dir():
|
|
507
596
|
meta = type("Meta", (), {"fields": "__all__", "model": model})
|
|
508
|
-
generated_class = type(
|
|
509
|
-
serializer_list_name,
|
|
510
|
-
(GenericSerializer,),
|
|
511
|
-
{
|
|
512
|
-
"Meta": meta,
|
|
513
|
-
},
|
|
514
|
-
)
|
|
515
|
-
globals()[serializer_list_name] = generated_class
|
|
516
|
-
|
|
517
597
|
generated_class = type(
|
|
518
598
|
serializer_name, (GenericSerializer,), {"Meta": meta}
|
|
519
599
|
)
|
|
@@ -159,6 +159,17 @@ def to_pascal_case(s):
|
|
|
159
159
|
return s.replace("_", " ").title().replace(" ", "")
|
|
160
160
|
|
|
161
161
|
|
|
162
|
+
def list_serializer_name(serializer_name):
|
|
163
|
+
"""Conventional list-serializer name for a detail serializer class name."""
|
|
164
|
+
suffix = "Serializer"
|
|
165
|
+
base = (
|
|
166
|
+
serializer_name[: -len(suffix)]
|
|
167
|
+
if serializer_name.endswith(suffix)
|
|
168
|
+
else serializer_name
|
|
169
|
+
)
|
|
170
|
+
return f"{base}ListSerializer"
|
|
171
|
+
|
|
172
|
+
|
|
162
173
|
def get_target_field(view, field, prefix=None):
|
|
163
174
|
field_name = getattr(field, "name", None)
|
|
164
175
|
related_model = getattr(field, "related_model", None)
|
|
@@ -78,53 +78,66 @@ class DRFReactBySchemaBaseModelViewSet(
|
|
|
78
78
|
|
|
79
79
|
def __init__(self, *args, **kwargs):
|
|
80
80
|
super().__init__(*args, **kwargs)
|
|
81
|
-
if not self.model:
|
|
81
|
+
if not self.model and self.model_name:
|
|
82
82
|
self.model = utils.get_model(self.app, self.model_name)
|
|
83
83
|
if self.app and self.serializer_class is not None:
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
module_name = (
|
|
85
|
+
"autocomplete_serializers"
|
|
86
|
+
if self.is_autocomplete
|
|
87
|
+
else "serializers"
|
|
88
|
+
)
|
|
86
89
|
try:
|
|
87
90
|
app_serializers = importlib.import_module(
|
|
88
|
-
f"{self.app}.
|
|
89
|
-
if not self.is_autocomplete
|
|
90
|
-
else f"{self.app}.autocomplete_serializers"
|
|
91
|
+
f"{self.app}.{module_name}"
|
|
91
92
|
)
|
|
92
|
-
except:
|
|
93
|
+
except ImportError:
|
|
93
94
|
return
|
|
94
95
|
|
|
95
|
-
|
|
96
|
-
app_serializers,
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
found += 1
|
|
101
|
-
|
|
102
|
-
if (
|
|
103
|
-
self.serializer_list_class
|
|
104
|
-
and app_serializer_name == self.serializer_list_class.__name__
|
|
105
|
-
):
|
|
106
|
-
self.serializer_list_class = app_serializer
|
|
107
|
-
found += 1
|
|
96
|
+
self.serializer_class = getattr(
|
|
97
|
+
app_serializers,
|
|
98
|
+
self.serializer_class.__name__,
|
|
99
|
+
self.serializer_class,
|
|
100
|
+
)
|
|
108
101
|
|
|
109
|
-
|
|
110
|
-
|
|
102
|
+
# The list serializer is optional: list/excel default to the general
|
|
103
|
+
# serializer, and an app-defined ``XListSerializer`` is honored when
|
|
104
|
+
# the viewset does not point at one explicitly.
|
|
105
|
+
list_name = (
|
|
106
|
+
self.serializer_list_class.__name__
|
|
107
|
+
if self.serializer_list_class is not None
|
|
108
|
+
else utils.list_serializer_name(self.serializer_class.__name__)
|
|
109
|
+
)
|
|
110
|
+
resolved_list_class = getattr(app_serializers, list_name, None)
|
|
111
|
+
if resolved_list_class is not None:
|
|
112
|
+
self.serializer_list_class = resolved_list_class
|
|
111
113
|
|
|
112
114
|
def get_schema_serializer_class(self):
|
|
113
115
|
"""Serializer describing the model shape (OPTIONS/TS generation).
|
|
114
116
|
|
|
115
117
|
Always the canonical ``serializer_class``: ``get_serializer_class`` may
|
|
116
|
-
return
|
|
117
|
-
but the schema must not change with the action.
|
|
118
|
+
return an explicit list serializer or a ``Meta.list_exclude`` projection
|
|
119
|
+
for ``list``/``excel``, but the schema must not change with the action.
|
|
118
120
|
"""
|
|
119
121
|
return self.serializer_class
|
|
120
122
|
|
|
121
|
-
def
|
|
122
|
-
"""
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
123
|
+
def get_list_serializer_class(self):
|
|
124
|
+
"""Serializer used by ``list``/``excel``.
|
|
125
|
+
|
|
126
|
+
An explicit ``serializer_list_class`` wins; otherwise a
|
|
127
|
+
``Meta.list_exclude`` projection of the general serializer is used; when
|
|
128
|
+
neither exists the general serializer itself serves the list.
|
|
129
|
+
"""
|
|
130
|
+
if self.serializer_list_class is not None:
|
|
127
131
|
return self.serializer_list_class
|
|
132
|
+
return (
|
|
133
|
+
serializers.list_serializer_for(self.serializer_class)
|
|
134
|
+
or self.serializer_class
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
def get_serializer_class(self):
|
|
138
|
+
"""Use the list serializer for ``list``/``excel``, if one applies."""
|
|
139
|
+
if getattr(self, "action", None) in ("list", "excel"):
|
|
140
|
+
return self.get_list_serializer_class()
|
|
128
141
|
return super().get_serializer_class()
|
|
129
142
|
|
|
130
143
|
def resolve_filter_callback(self, callback_attr):
|
|
@@ -791,9 +804,6 @@ for app in settings["APPS"]:
|
|
|
791
804
|
"serializer_class": getattr(
|
|
792
805
|
serializers, f"{model_name}Serializer"
|
|
793
806
|
),
|
|
794
|
-
"serializer_list_class": getattr(
|
|
795
|
-
serializers, f"{model_name}ListSerializer"
|
|
796
|
-
),
|
|
797
807
|
},
|
|
798
808
|
)
|
|
799
809
|
globals()[viewset_name] = generated_class
|
|
@@ -824,11 +834,6 @@ for app in settings["APPS"]:
|
|
|
824
834
|
"serializer_class": getattr(
|
|
825
835
|
serializers, f"{related_model_name}Serializer"
|
|
826
836
|
),
|
|
827
|
-
"serializer_list_class": getattr(
|
|
828
|
-
serializers,
|
|
829
|
-
f"{related_model_name}ListSerializer",
|
|
830
|
-
None,
|
|
831
|
-
),
|
|
832
837
|
"related_name": field.name,
|
|
833
838
|
"many": True,
|
|
834
839
|
"parent_model": model,
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema.egg-info/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: drf-react-by-schema
|
|
3
|
-
Version: 0.28.
|
|
3
|
+
Version: 0.28.1
|
|
4
4
|
Summary: A Django app to easily integrate with React from models.
|
|
5
5
|
Home-page: https://gitlab.com/eita/drf-react-by-schema/drf-react-by-schema-django
|
|
6
6
|
Author: Cooperativa EITA
|
|
@@ -252,6 +252,39 @@ Quick start
|
|
|
252
252
|
|
|
253
253
|
You can customize ``viewsets`` and ``serializers`` to annotate other attributes.
|
|
254
254
|
|
|
255
|
+
List vs. detail serializers
|
|
256
|
+
---------------------------
|
|
257
|
+
The generated ``XSerializer`` is the canonical serializer and serves both ``retrieve``
|
|
258
|
+
and ``list``/``excel``. When a detail-only field would be expensive in lists, declare it
|
|
259
|
+
in ``Meta.list_exclude`` on the app's general serializer:
|
|
260
|
+
|
|
261
|
+
.. code-block:: python
|
|
262
|
+
|
|
263
|
+
from drf_react_by_schema.serializers import (
|
|
264
|
+
ClienteSerializer as BaseClienteSerializer,
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
class ClienteSerializer(BaseClienteSerializer):
|
|
268
|
+
contratacao = drf_serializers.SerializerMethodField() # detail only
|
|
269
|
+
nome = drf_serializers.CharField(read_only=True) # both
|
|
270
|
+
|
|
271
|
+
class Meta(BaseClienteSerializer.Meta):
|
|
272
|
+
list_exclude = ("contratacao",)
|
|
273
|
+
|
|
274
|
+
The package derives (and caches) a list projection that keeps every other field,
|
|
275
|
+
including future additions to the general serializer. ``list_exclude`` takes serializer
|
|
276
|
+
field names, not model ``source`` paths; ``()`` means "the list is identical to the
|
|
277
|
+
detail response". An explicit list serializer remains available as an escape hatch:
|
|
278
|
+
|
|
279
|
+
.. code-block:: python
|
|
280
|
+
|
|
281
|
+
class ClienteViewSet(GenericViewSet):
|
|
282
|
+
serializer_class = ClienteSerializer
|
|
283
|
+
serializer_list_class = ClienteListSerializer # optional, full override
|
|
284
|
+
|
|
285
|
+
``XListSerializer`` classes are no longer generated automatically: the default is the
|
|
286
|
+
general serializer, so app-added fields can never silently disappear from lists.
|
|
287
|
+
|
|
255
288
|
Global permission-aware queryset filtering
|
|
256
289
|
------------------------------------------
|
|
257
290
|
Every generated viewset already supports a ``filter_by_callback`` (and a list-only
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/__init__.py
RENAMED
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/array.py
RENAMED
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/boolean.py
RENAMED
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/date.py
RENAMED
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/file.py
RENAMED
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/geometry.py
RENAMED
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/number.py
RENAMED
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/relational.py
RENAMED
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/serializers.py
RENAMED
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/fields/text.py
RENAMED
|
File without changes
|
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/management/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/migrations/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/patches/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/schema_bundle.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema/utils_cache.py
RENAMED
|
File without changes
|
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema.egg-info/SOURCES.txt
RENAMED
|
File without changes
|
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema.egg-info/requires.txt
RENAMED
|
File without changes
|
{drf_react_by_schema-0.28.0 → drf_react_by_schema-0.28.1}/drf_react_by_schema.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|