How to change size of a textfield for Admin

348 views
Skip to first unread message

bcurtu

unread,
Aug 1, 2008, 6:13:42 AM8/1/08
to Django users
I have realized models.CharFields have a fixed widget (input type...).
Is it possible to modify the size of the widget with css?

Cheers.

Gonzalo Delgado

unread,
Aug 1, 2008, 6:47:09 AM8/1/08
to django...@googlegroups.com
El Fri, 1 Aug 2008 03:13:42 -0700 (PDT)
bcurtu <bcu...@gmail.com> escribió:

> I have realized models.CharFields have a fixed widget (input type...).
> Is it possible to modify the size of the widget with css?

http://www.djangoproject.com/documentation/admin_css/

Firebug is great for trying out CSS changes in real-time.

--
P.U. Gonzalo Delgado <gonzalo...@fortix.com.ar>
http://djangopeople.net/gonzalo/

Daniel Roseman

unread,
Aug 1, 2008, 8:50:32 AM8/1/08
to Django users
You can use CSS, or override the form in your admin.py:

class MyAdminForm(forms.ModelForm):
mytextfield = forms.CharField(widget=forms.TextInput(attrs={'size':
35})

class MyAdminOptions(admin.ModelAdmin):
form = MyAdminForm

admin.site.register(MyModel, MyAdminOptions)

--
DR.

Eric Abrahamsen

unread,
Aug 1, 2008, 10:53:55 AM8/1/08
to django...@googlegroups.com
On Aug 1, 2008, at 8:50 PM, Daniel Roseman wrote:

>
> On Aug 1, 11:13 am, bcurtu <bcu...@gmail.com> wrote:
>> I have realized models.CharFields have a fixed widget (input
>> type...).
>> Is it possible to modify the size of the widget with css?
>>
>> Cheers.

I actually felt like all the charfields and textareas in Forms were
too small (must have gotten used to the old admin), and I went and
changed the source code. Hardly counts as a hack, I think, as it was
only two spots...

bcurtu

unread,
Aug 1, 2008, 1:16:35 PM8/1/08
to Django users
Hi all,

Thanks for your answers.
Daniel: I can not try that code right now, but I think the attribute
widget is not valid in models.CharField. Anyway, I will try it on
monday...
Eric: You're right. I have alredy "hacked" in this way. However it's
not a good solution, because next time we sync to django we will loose
our changes!

Eric Abrahamsen

unread,
Aug 1, 2008, 1:28:56 PM8/1/08
to django...@googlegroups.com

On Aug 2, 2008, at 1:16 AM, bcurtu wrote:

>
> Hi all,
>
> Thanks for your answers.
> Daniel: I can not try that code right now, but I think the attribute
> widget is not valid in models.CharField. Anyway, I will try it on
> monday...
> Eric: You're right. I have alredy "hacked" in this way. However it's
> not a good solution, because next time we sync to django we will loose
> our changes!

Thanks to the miracle of versioning software, you should be okay. Are
you using subversion? When you run 'svn up', there shouldn't be any
conflicts unless the new version of trunk contains changes to the
exact line that you edited in the source code. When you update your
code, run 'svn status -u' first to see if there are updates to the
file which you've modified. If so, take a closer look at the updates
to see if they'll create a conflict...

Yours,
Eric

Daniel Roseman

unread,
Aug 1, 2008, 2:25:07 PM8/1/08
to Django users
On Aug 1, 6:16 pm, bcurtu <bcu...@gmail.com> wrote:
> Hi all,
>
> Thanks for your answers.
> Daniel: I can not try that code right now, but I think the attribute
> widget is not valid in models.CharField. Anyway, I will try it on
> monday...
> Eric: You're right. I have alredy "hacked" in this way. However it's
> not a good solution, because next time we sync to django we will loose
> our changes!
>

This isn't in models.CharField, but in *forms*.CharField.

The way you customise your admin site is to define a model form and
override some of the fields, as I demonstrated in my snippet. This is
on trunk/alpha/newforms-admin, of course.
--
DR.

bcurtu

unread,
Aug 4, 2008, 4:55:34 AM8/4/08
to Django users
Daniel's solution was good too, however, it was too "expensive",
having to override all model fields. My solution:
To override get_form method and to change the fields you want

class MyClassAdmin(admin.ModelAdmin):
...
def get_form(self, request, obj=None):
form= super(EventAdmin,self).get_form(request, obj=None)
name = forms.CharField(widget=forms.TextInput(attrs={'size':
100, 'max_length':200}),required=True,)
form.base_fields['name']=name
return form

Cheers
Reply all
Reply to author
Forward
0 new messages