mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
Fixed #12404 -- Improved model validation for CharField and DecimalField. Thanks to tiliv for the report, josh for the patch, and Leo for the tests.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12768 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -37,13 +37,26 @@ def get_validation_errors(outfile, app=None):
|
||||
e.add(opts, '"%s": You can\'t use "id" as a field name, because each model automatically gets an "id" field if none of the fields have primary_key=True. You need to either remove/rename your "id" field or add primary_key=True to a field.' % f.name)
|
||||
if f.name.endswith('_'):
|
||||
e.add(opts, '"%s": Field names cannot end with underscores, because this would lead to ambiguous queryset filters.' % f.name)
|
||||
if isinstance(f, models.CharField) and f.max_length in (None, 0):
|
||||
e.add(opts, '"%s": CharFields require a "max_length" attribute.' % f.name)
|
||||
if isinstance(f, models.CharField):
|
||||
try:
|
||||
max_length = int(f.max_length)
|
||||
if max_length <= 0:
|
||||
e.add(opts, '"%s": CharFields require a "max_length" attribute that is a positive integer.' % f.name)
|
||||
except (ValueError, TypeError):
|
||||
e.add(opts, '"%s": CharFields require a "max_length" attribute that is a positive integer.' % f.name)
|
||||
if isinstance(f, models.DecimalField):
|
||||
if f.decimal_places is None:
|
||||
e.add(opts, '"%s": DecimalFields require a "decimal_places" attribute.' % f.name)
|
||||
if f.max_digits is None:
|
||||
e.add(opts, '"%s": DecimalFields require a "max_digits" attribute.' % f.name)
|
||||
try:
|
||||
decimal_places = int(f.decimal_places)
|
||||
if decimal_places <= 0:
|
||||
e.add(opts, '"%s": DecimalFields require a "decimal_places" attribute that is a positive integer.' % f.name)
|
||||
except (ValueError, TypeError):
|
||||
e.add(opts, '"%s": DecimalFields require a "decimal_places" attribute that is a positive integer.' % f.name)
|
||||
try:
|
||||
max_digits = int(f.max_digits)
|
||||
if max_digits <= 0:
|
||||
e.add(opts, '"%s": DecimalFields require a "max_digits" attribute that is a positive integer.' % f.name)
|
||||
except (ValueError, TypeError):
|
||||
e.add(opts, '"%s": DecimalFields require a "max_digits" attribute that is a positive integer.' % f.name)
|
||||
if isinstance(f, models.FileField) and not f.upload_to:
|
||||
e.add(opts, '"%s": FileFields require an "upload_to" attribute.' % f.name)
|
||||
if isinstance(f, models.ImageField):
|
||||
|
||||
Reference in New Issue
Block a user