I am wondering what I need to do in order to get the admin site to
enforce the maxlength property. I am using the MR Branch.
I have a model with a CharField where maxlength=3. I goto in admin. It
shows a huge box ie way more than 3 charcaaters, allows you to enter
more than 3 characters and doesnt error when you save more than 3
characters.
This seems strange to me. Any suggestions ?
Thanks,
C
Hi Chaos,
CharFields are represented in the admin with a fixed length. The
"maxlength" property on those boxes is set correctly, but the size of
the actual form element is hard-coded.
This is probably worth changing, because you're right to note that
it's silly for a two-character field to have a long input element. But
we can't just blindly set the length of the field to the maxlength,
because maxlength=200 would result in a horribly long field. There
would have to be some sort of upper limit.
Adrian
--
Adrian Holovaty
holovaty.com | djangoproject.com
If the form element is set correctly I have to question why it saved
more than 3 characters to the DB and then retrieved those same
characters back again. I would have to dig deeper to see exactly what
is going on which is why I posted the question first, under the idea
that other people have want the same restrictions.
When i have time I will investigate further.
Thanks,
C
As lame as it sounds, you're right. There isn't a clean way to do it.
I know it was discussed on the list not too long ago (or maybe it was
django-dev), but I don't think much came out of it. Here's a link
anyhow:
That said, if you can come up with a nice way to do this, patches and
ideas are welcome.
Joseph
Wow... that's this thread! (Time for me to sleep.)
> That said, if you can come up with a nice way to do this, patches and
> ideas are welcome.
This still applies :)
Best of all, you only have to modify one file -- core/meta/fields.py:
1) Add a new param "length=None" to the __init__ declaration for the
base Field class, around lines 105-111
2) in the __init__ method, add a line to use the new param:
"self.length = length" (I added this right after the line that grabs
the maxlength param, cause it made sense to me there)
3) tweak the get_manipulator_fields method around line 230 to make use
of the new "self.length" attribute. Add the following lines (with
corrected indenting, of course):
if self.length:
params['length'] = self.length
You should end up with something like:
if self.maxlength and not self.choices:
params['maxlength'] = self.maxlength
if self.length:
params['length'] = self.length
That's it. Now you can use a "length" param in your model
declarations, like:
class Poll(meta.Model):
question = meta.CharField(maxlength=200, length=4)
The result is what you'd expect: the resulting HTML form field used in
the admin interface has its "size" attribute set to "4".