maxlength in the Admin site

31 views
Skip to first unread message

ChaosKCW

unread,
Mar 21, 2006, 9:53:58 AM3/21/06
to Django users
Hi

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

Adrian Holovaty

unread,
Mar 22, 2006, 10:57:24 AM3/22/06
to django...@googlegroups.com

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

ChaosKCW

unread,
Mar 23, 2006, 9:47:53 AM3/23/06
to Django users
Hi

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

argus

unread,
Apr 19, 2006, 11:26:28 PM4/19/06
to Django users
Is there any reason that there's not a way to specify/override the size
of the form field that gets used in templates (separately from what
gets used in the admin interface)? I can't think of one, and it's been
something of a thorn in my side recently, so I figured I'd ask :) I'm
reluctant to hack into the Django source for something so trivial, but
I'm getting closer to doing so..

Joseph Kocherhans

unread,
Apr 19, 2006, 11:48:08 PM4/19/06
to django...@googlegroups.com

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:

http://groups.google.com/group/django-users/browse_frm/thread/4f6e2e0c936dcca5/7e7c0a7b2b168bd5?tvc=1&q=field+length#7e7c0a7b2b168bd5

That said, if you can come up with a nice way to do this, patches and
ideas are welcome.

Joseph

Joseph Kocherhans

unread,
Apr 19, 2006, 11:54:09 PM4/19/06
to django...@googlegroups.com
On 4/19/06, Joseph Kocherhans <jkoch...@gmail.com> wrote:
> On 4/19/06, argus <rmorr...@gmail.com> wrote:
> >
> > Is there any reason that there's not a way to specify/override the size
> > of the form field that gets used in templates (separately from what
> > gets used in the admin interface)? I can't think of one, and it's been
> > something of a thorn in my side recently, so I figured I'd ask :) I'm
> > reluctant to hack into the Django source for something so trivial, but
> > I'm getting closer to doing so..
>
> 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:
>
> http://groups.google.com/group/django-users/browse_frm/thread/4f6e2e0c936dcca5/7e7c0a7b2b168bd5?tvc=1&q=field+length#7e7c0a7b2b168bd5

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 :)

argus

unread,
Apr 20, 2006, 2:09:10 AM4/20/06
to Django users
Actually, it was really easy. As in "four lines" easy. I haven't done
extensive testing with it, but a quick run through seems to demonstrate
that it does what I want it to do.

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".

argus

unread,
Apr 20, 2006, 10:04:32 PM4/20/06
to Django users
Well, for better or worse I submitted a ticket with my modded
fields.py, based off the latest SVN trunk:
http://code.djangoproject.com/ticket/1665

ChaosKCW

unread,
Apr 25, 2006, 1:28:35 PM4/25/06
to Django users
Sweet, thanks alot. A welcome addition.

Reply all
Reply to author
Forward
0 new messages