Opened 9 years ago

Closed 9 years ago

#24818 closed Bug (fixed)

models.CharField shouldn't accept a string as max_length

Reported by: Santiago L Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8
Severity: Normal Keywords:
Cc: Santiago L Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In models.CharField you can define max_length as string and the system check framework doesn't complain (related mailing list thread):

from django.db import models

class Foo(models.Model):
    bar = models.CharField(max_length='16')
>>> # following code raises an Exception
>>> obj = Foo(bar='lorem ipsum')
>>> obj.clean_fields()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/base.py", line 1167, in clean_fields
    setattr(self, f.attname, f.clean(raw_value, self))
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/fields/__init__.py", line 589, in clean
    self.run_validators(value)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/fields/__init__.py", line 541, in run_validators
    v(value)
  File "/usr/local/lib/python3.4/dist-packages/django/core/validators.py", line 280, in __call__
    if self.compare(cleaned, self.limit_value):
  File "/usr/local/lib/python3.4/dist-packages/django/core/validators.py", line 319, in <lambda>
    compare = lambda self, a, b: a > b
TypeError: unorderable types: int() > str()

Change History (8)

comment:1 by Santiago L, 9 years ago

Related to #20440.

comment:2 by Alasdair Nicol, 9 years ago

Has patch: set

comment:3 by Alasdair Nicol, 9 years ago

Summary: models.CharField shouldn't accept a string as max_lenghtmodels.CharField shouldn't accept a string as max_length

comment:4 by Alasdair Nicol, 9 years ago

Triage Stage: UnreviewedAccepted

comment:5 by Baptiste Mispelon, 9 years ago

Is there a reason why we can't cast max_length to an integer value in Field.__init__?

in reply to:  5 comment:6 by Santiago L, 9 years ago

Replying to bmispelon:

Is there a reason why we can't cast max_length to an integer value in Field.__init__?

Because that will mask the real "problem" as has happened with the current implementation that casts mask_length at _check_max_length_attribute .

IMHO, Django shouldn't handle user's fault but warn the user about it.

comment:7 by Santiago L, 9 years ago

Cc: Santiago L added

comment:8 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: newclosed

In d091b75:

Fixed #24818 -- Prevented models.CharField from accepting a string as max_length

Note: See TracTickets for help on using tickets.
Back to Top