general.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import warnings
  2. from django.contrib.postgres.fields import ArrayField
  3. from django.db.models import Aggregate, BooleanField, JSONField
  4. from django.db.models import StringAgg as _StringAgg
  5. from django.db.models import Value
  6. from django.utils.deprecation import RemovedInDjango70Warning
  7. from .mixins import _DeprecatedOrdering
  8. __all__ = [
  9. "ArrayAgg",
  10. "BitAnd",
  11. "BitOr",
  12. "BitXor",
  13. "BoolAnd",
  14. "BoolOr",
  15. "JSONBAgg",
  16. "StringAgg", # RemovedInDjango70Warning.
  17. ]
  18. # RemovedInDjango61Warning: When the deprecation ends, replace with:
  19. # class ArrayAgg(Aggregate):
  20. class ArrayAgg(_DeprecatedOrdering, Aggregate):
  21. function = "ARRAY_AGG"
  22. allow_distinct = True
  23. allow_order_by = True
  24. @property
  25. def output_field(self):
  26. return ArrayField(self.source_expressions[0].output_field)
  27. class BitAnd(Aggregate):
  28. function = "BIT_AND"
  29. class BitOr(Aggregate):
  30. function = "BIT_OR"
  31. class BitXor(Aggregate):
  32. function = "BIT_XOR"
  33. class BoolAnd(Aggregate):
  34. function = "BOOL_AND"
  35. output_field = BooleanField()
  36. class BoolOr(Aggregate):
  37. function = "BOOL_OR"
  38. output_field = BooleanField()
  39. # RemovedInDjango61Warning: When the deprecation ends, replace with:
  40. # class JSONBAgg(Aggregate):
  41. class JSONBAgg(_DeprecatedOrdering, Aggregate):
  42. function = "JSONB_AGG"
  43. allow_distinct = True
  44. allow_order_by = True
  45. output_field = JSONField()
  46. # RemovedInDjango61Warning: When the deprecation ends, replace with:
  47. # class StringAgg(_StringAgg):
  48. # RemovedInDjango70Warning: When the deprecation ends, remove completely.
  49. class StringAgg(_DeprecatedOrdering, _StringAgg):
  50. def __init__(self, expression, delimiter, **extra):
  51. if isinstance(delimiter, str):
  52. warnings.warn(
  53. "delimiter: str will be resolved as a field reference instead "
  54. "of a string literal on Django 7.0. Pass "
  55. f"`delimiter=Value({delimiter!r})` to preserve the previous behaviour.",
  56. category=RemovedInDjango70Warning,
  57. stacklevel=2,
  58. )
  59. delimiter = Value(delimiter)
  60. warnings.warn(
  61. "The PostgreSQL specific StringAgg function is deprecated. Use "
  62. "django.db.models.aggregate.StringAgg instead.",
  63. category=RemovedInDjango70Warning,
  64. stacklevel=2,
  65. )
  66. super().__init__(expression, delimiter, **extra)