@@ -1725,54 +1725,84 @@ def check(self, **kwargs):
17251725 return errors
17261726
17271727 def _check_decimal_places (self ):
1728- try :
1729- decimal_places = int (self .decimal_places )
1730- if decimal_places < 0 :
1731- raise ValueError ()
1732- except TypeError :
1733- return [
1734- checks .Error (
1735- "DecimalFields must define a 'decimal_places' attribute." ,
1736- obj = self ,
1737- id = "fields.E130" ,
1738- )
1739- ]
1740- except ValueError :
1741- return [
1742- checks .Error (
1743- "'decimal_places' must be a non-negative integer." ,
1744- obj = self ,
1745- id = "fields.E131" ,
1746- )
1747- ]
1728+ if self .decimal_places is None :
1729+ if (
1730+ not connection .features .supports_no_precision_decimalfield
1731+ and "supports_no_precision_decimalfield"
1732+ not in self .model ._meta .required_db_features
1733+ ):
1734+ return [
1735+ checks .Error (
1736+ "DecimalFields must define a 'decimal_places' attribute." ,
1737+ obj = self ,
1738+ id = "fields.E130" ,
1739+ )
1740+ ]
1741+ elif self .max_digits is not None :
1742+ return [
1743+ checks .Error (
1744+ "DecimalField’s max_digits and decimal_places must both "
1745+ "be defined or both omitted." ,
1746+ obj = self ,
1747+ id = "fields.E135" ,
1748+ ),
1749+ ]
17481750 else :
1749- return []
1751+ try :
1752+ decimal_places = int (self .decimal_places )
1753+ if decimal_places < 0 :
1754+ raise ValueError ()
1755+ except ValueError :
1756+ return [
1757+ checks .Error (
1758+ "'decimal_places' must be a non-negative integer." ,
1759+ obj = self ,
1760+ id = "fields.E131" ,
1761+ )
1762+ ]
1763+ return []
17501764
17511765 def _check_max_digits (self ):
1752- try :
1753- max_digits = int (self .max_digits )
1754- if max_digits <= 0 :
1755- raise ValueError ()
1756- except TypeError :
1757- return [
1758- checks .Error (
1759- "DecimalFields must define a 'max_digits' attribute." ,
1760- obj = self ,
1761- id = "fields.E132" ,
1762- )
1763- ]
1764- except ValueError :
1765- return [
1766- checks .Error (
1767- "'max_digits' must be a positive integer." ,
1768- obj = self ,
1769- id = "fields.E133" ,
1770- )
1771- ]
1766+ if self .max_digits is None :
1767+ if (
1768+ not connection .features .supports_no_precision_decimalfield
1769+ and "supports_no_precision_decimalfield"
1770+ not in self .model ._meta .required_db_features
1771+ ):
1772+ return [
1773+ checks .Error (
1774+ "DecimalFields must define a 'max_digits' attribute." ,
1775+ obj = self ,
1776+ id = "fields.E132" ,
1777+ )
1778+ ]
1779+ elif self .decimal_places is not None :
1780+ return [
1781+ checks .Error (
1782+ "DecimalField’s max_digits and decimal_places must both "
1783+ "be defined or both omitted." ,
1784+ obj = self ,
1785+ id = "fields.E135" ,
1786+ ),
1787+ ]
17721788 else :
1773- return []
1789+ try :
1790+ max_digits = int (self .max_digits )
1791+ if max_digits <= 0 :
1792+ raise ValueError ()
1793+ except ValueError :
1794+ return [
1795+ checks .Error (
1796+ "'max_digits' must be a positive integer." ,
1797+ obj = self ,
1798+ id = "fields.E133" ,
1799+ )
1800+ ]
1801+ return []
17741802
17751803 def _check_decimal_places_and_max_digits (self , ** kwargs ):
1804+ if self .decimal_places is None or self .max_digits is None :
1805+ return []
17761806 if int (self .decimal_places ) > int (self .max_digits ):
17771807 return [
17781808 checks .Error (
0 commit comments