On Thu, Oct 15, 2009 at 12:30 PM, Gerard <lijss...@gp-net.nl> wrote:
> Hi All,
> It seems the max_lenght from the model definition is not respected > thoughout > the form that's created when using Modelform.
> company_name = models.CharField(max_length=75)
> Is this correct or am I missing something?
This is not correct. For this model:
class Tag(models.Model): name = models.CharField(max_length=4)
I see max_length being respected in the corresponding ModelForm:
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole)
>>> from ttt.models import Tag >>> from django import forms >>> class TForm(forms.ModelForm):
From my experience, max_length is not taken into account when you
override model field in form field. In this case I don't know of any
trivial way to read other field properties from model description.
Regards, Reinis
On 20 okt., 17:54, Karen Tracey <kmtra...@gmail.com> wrote:
> class Tag(models.Model):
> name = models.CharField(max_length=4)
> I see max_length being respected in the corresponding ModelForm:
> Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
> [GCC 4.3.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> (InteractiveConsole)>>> from ttt.models import Tag
> >>> from django import forms
> >>> class TForm(forms.ModelForm):
> ... class Meta:
> ... model = Tag
> ...>>> tf = TForm({'name':'Too long'})
> >>> tf.is_valid()
> False
> >>> tf.errors
> {'name': [u'Ensure this value has at most 4 characters (it has 8).']}
> >>> quit()
> What exactly did you try that led you to the conclusion that max_length is
> not respected by ModelForms?
> From my experience, max_length is not taken into account when you
> override model field in form field.
This is (and was, when I first time faced it) quite obvious for me .
If ModelForm was modifying overridden fields, the whole concept
of overriding would be useless.
On the other hand it would be nice if we could write something like:
Tomasz, it was obvious for me too, however, I had defined a form,
which had some fields overridden, but some not- I was hoping that
options for fields that were not overridden were preserved, but due to
specific way admin interface handles forms, required class wasn't
added to my custom form fields. That's why I thought other field
options were not taken into account.
Reinis
On 7 nov., 21:27, Tomasz Zieliński <tomasz.zielin...@pyconsultant.eu>
wrote:
> On 7 Lis, 18:25, fest <hell...@gmail.com> wrote:
> > From my experience, max_length is not taken into account when you
> > override modelfieldin formfield.
> This is (and was, when I first time faced it) quite obvious for me .
> IfModelFormwas modifying overridden fields, the whole concept
> of overriding would be useless.
> On the other hand it would be nice if we could write something like: