(self, condition=None, then=None, **lookups)
| 1639 | conditional = False |
| 1640 | |
| 1641 | def __init__(self, condition=None, then=None, **lookups): |
| 1642 | if lookups: |
| 1643 | if invalid_kwargs := PROHIBITED_FILTER_KWARGS.intersection(lookups): |
| 1644 | invalid_str = ", ".join(f"'{k}'" for k in sorted(invalid_kwargs)) |
| 1645 | raise TypeError(f"The following kwargs are invalid: {invalid_str}") |
| 1646 | if condition is None: |
| 1647 | condition, lookups = Q(**lookups), None |
| 1648 | elif getattr(condition, "conditional", False): |
| 1649 | condition, lookups = Q(condition, **lookups), None |
| 1650 | if condition is None or not getattr(condition, "conditional", False) or lookups: |
| 1651 | raise TypeError( |
| 1652 | "When() supports a Q object, a boolean expression, or lookups " |
| 1653 | "as a condition." |
| 1654 | ) |
| 1655 | if isinstance(condition, Q) and not condition: |
| 1656 | raise ValueError("An empty Q() can't be used as a When() condition.") |
| 1657 | super().__init__(output_field=None) |
| 1658 | self.condition = condition |
| 1659 | self.result = self._parse_expressions(then)[0] |
| 1660 | |
| 1661 | def __str__(self): |
| 1662 | return "WHEN %r THEN %r" % (self.condition, self.result) |
nothing calls this directly
no test coverage detected