Opened 8 years ago

Closed 5 years ago

Last modified 3 years ago

#8263 closed Uncategorized (invalid)

models.TextField max_length

Reported by: osanha Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: TextField, max_length
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


if some model has file type of models.CharField.

When field string was overflow max_length, ProgramError was raised.

But, if model has field type of models.TextField in same case.

Nothing was raised.

In this case, max_length seems to be a useless thing.

Change History (4)

comment:1 Changed 8 years ago by jacob

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Text fields have no maximum length -- that's the whole point of text fields. If you want length checking, use CharField.

comment:2 Changed 5 years ago by anonymous

  • Resolution invalid deleted
  • Status changed from closed to reopened

Then why does TextField accept a max_length parameter?

comment:3 Changed 5 years ago by gabrielhurley

  • Resolution set to invalid
  • Status changed from reopened to closed

You can pass max_length as a keyword argument to any model field that inherits from Field; that doesn't mean that the class actually does anything with it. Nowhere that I see in the docs or code indicate that it will work.

comment:4 Changed 3 years ago by vkstoykov@…

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

Everybody that has database knowledge knows that in database TextFields are not restricted by length, but django Models are used to define domain logic in your application like in MVC pattern. I know that Django tells us that it does not follow MVC instead it follows MTV but M in MVC and M in MTV is the same thing.

In Django there is a fields that has programmatically defined restrictions like URLField and EmailField. Also for django model fields is possible to define validators, which are also programmatically restrictions. This is domain logic of the application. In the database I can put not valid URLs and not valid E-Mail addresses or values that will not pass django validators. These fields are created for easy validation in forms.

If user want to use TextField (because of default widget) it will not harm if TextField accept max_length argument and this argument is used for form validation. And by this simple thing also the DRY principle will be followed when someone wants field that will look as Textarea but will have restricted length.

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