﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
22305	MaxLengthValidator doesn't take database encoding into account	Joeri Bekker	nobody	"Interesting issue we came across today. Consider the following:

{{{
>>> from django.db import models
>>> from django.forms.models import modelform_factory

>>> class Pizza(models.Model):
>>>    name = models.CharField(max_length=10)  # Short pizza names ftw.

>>> form = modelform_factory(Pizza)

>>> pizza_name = u'mozzarélla'  # Note the special character.
>>> f = form(data={'name': pizza_name})
>>> f.is_valid()
True
}}}

However, when form is saved to the database you will see: {{{DataError: value too long for type character varying(10)}}}.

Why? Because the form's {{{MaxLengthValidator}}} sees:
{{{
>>> len(pizza_name)  # Woop, it fits!
10
}}}

And the database sees (assuming it uses UTF-8 encoding) this:
{{{
>>> len(pizza_name.encode('utf-8'))  # Oops, does not fit!
11
}}}

A solution would be to replace {{{len(x)}}} in the {{{MaxLengthValidator}}} to {{{len(x.encode('utf-8'))}}}. This however does not take the actual database encoding into account..."	Cleanup/optimization	closed	Documentation	dev	Normal	fixed		Joeri Bekker	Accepted	1	0	0	0	0	0
