|
5 | 5 | import inspect
|
6 | 6 | import re
|
7 | 7 | import uuid
|
| 8 | +import warnings |
8 | 9 | from collections import OrderedDict
|
9 | 10 | from collections.abc import Mapping
|
10 | 11 |
|
@@ -249,31 +250,41 @@ class CreateOnlyDefault:
|
249 | 250 | for create operations, but that do not return any value for update
|
250 | 251 | operations.
|
251 | 252 | """
|
| 253 | + requires_context = True |
| 254 | + |
252 | 255 | def __init__(self, default):
|
253 | 256 | self.default = default
|
254 | 257 |
|
255 |
| - def set_context(self, serializer_field): |
256 |
| - self.is_update = serializer_field.parent.instance is not None |
257 |
| - if callable(self.default) and hasattr(self.default, 'set_context') and not self.is_update: |
258 |
| - self.default.set_context(serializer_field) |
259 |
| - |
260 |
| - def __call__(self): |
261 |
| - if self.is_update: |
| 258 | + def __call__(self, serializer_field): |
| 259 | + is_update = serializer_field.parent.instance is not None |
| 260 | + if is_update: |
262 | 261 | raise SkipField()
|
263 | 262 | if callable(self.default):
|
264 |
| - return self.default() |
| 263 | + if hasattr(self.default, 'set_context'): |
| 264 | + warnings.warn( |
| 265 | + "Method `set_context` on defaults is deprecated and will " |
| 266 | + "no longer be called starting with 3.12. Instead set " |
| 267 | + "`requires_context = True` on the class, and accept the " |
| 268 | + "context as an additional argument.", |
| 269 | + DeprecationWarning, stacklevel=2 |
| 270 | + ) |
| 271 | + self.default.set_context(self) |
| 272 | + |
| 273 | + if getattr(self.default, 'requires_context', False): |
| 274 | + return self.default(serializer_field) |
| 275 | + else: |
| 276 | + return self.default() |
265 | 277 | return self.default
|
266 | 278 |
|
267 | 279 | def __repr__(self):
|
268 | 280 | return '%s(%s)' % (self.__class__.__name__, repr(self.default))
|
269 | 281 |
|
270 | 282 |
|
271 | 283 | class CurrentUserDefault:
|
272 |
| - def set_context(self, serializer_field): |
273 |
| - self.user = serializer_field.context['request'].user |
| 284 | + requires_context = True |
274 | 285 |
|
275 |
| - def __call__(self): |
276 |
| - return self.user |
| 286 | + def __call__(self, serializer_field): |
| 287 | + return serializer_field.context['request'].user |
277 | 288 |
|
278 | 289 | def __repr__(self):
|
279 | 290 | return '%s()' % self.__class__.__name__
|
@@ -489,8 +500,20 @@ def get_default(self):
|
489 | 500 | raise SkipField()
|
490 | 501 | if callable(self.default):
|
491 | 502 | if hasattr(self.default, 'set_context'):
|
| 503 | + warnings.warn( |
| 504 | + "Method `set_context` on defaults is deprecated and will " |
| 505 | + "no longer be called starting with 3.12. Instead set " |
| 506 | + "`requires_context = True` on the class, and accept the " |
| 507 | + "context as an additional argument.", |
| 508 | + DeprecationWarning, stacklevel=2 |
| 509 | + ) |
492 | 510 | self.default.set_context(self)
|
493 |
| - return self.default() |
| 511 | + |
| 512 | + if getattr(self.default, 'requires_context', False): |
| 513 | + return self.default(self) |
| 514 | + else: |
| 515 | + return self.default() |
| 516 | + |
494 | 517 | return self.default
|
495 | 518 |
|
496 | 519 | def validate_empty_values(self, data):
|
@@ -551,10 +574,20 @@ def run_validators(self, value):
|
551 | 574 | errors = []
|
552 | 575 | for validator in self.validators:
|
553 | 576 | if hasattr(validator, 'set_context'):
|
| 577 | + warnings.warn( |
| 578 | + "Method `set_context` on validators is deprecated and will " |
| 579 | + "no longer be called starting with 3.12. Instead set " |
| 580 | + "`requires_context = True` on the class, and accept the " |
| 581 | + "context as an additional argument.", |
| 582 | + DeprecationWarning, stacklevel=2 |
| 583 | + ) |
554 | 584 | validator.set_context(self)
|
555 | 585 |
|
556 | 586 | try:
|
557 |
| - validator(value) |
| 587 | + if getattr(validator, 'requires_context', False): |
| 588 | + validator(value, self) |
| 589 | + else: |
| 590 | + validator(value) |
558 | 591 | except ValidationError as exc:
|
559 | 592 | # If the validation error contains a mapping of fields to
|
560 | 593 | # errors then simply raise it immediately rather than
|
|
0 commit comments