简体   繁体   English

在Django管理中更改外键小部件的大小

[英]Change size of foreign key widget in django admin

I've spent the last two days trying to do this, and I can't figure it out. 我花了最后两天的时间来尝试执行此操作,但我无法解决。 I'm using grappelli, and I've tried several things. 我正在使用grappelli,并且已经尝试了一些方法。

I tried specifying the widget: 我尝试指定小部件:

class MyForm(ModelForm):
    class Meta:
        model = MyModel
        widgets = {
            'foreign_key': widgets.TextInput(attrs={'width': '200'}),
        }

In this case, it just seemed to be ignored. 在这种情况下,它似乎只是被忽略了。

I also tried adding css using a Media definition. 我也尝试使用Media定义添加CSS。 This got loaded, but inspecting the element in my browse showed that it was overridden by the grappelli css. 这已加载,但是检查我的浏览器中的元素显示该元素已被grappelli CSS覆盖。

Is there an easy way of doing this that I've missed? 有没有一种我错过的简便方法?

I would re-try adding css using the media definition but this time, put a !important to your css attribute. 我将尝试使用媒体定义重新添加CSS,但是这次,将!important放入您的CSS属性。

.widget input {
    width: 200px !important;
}

From http://en.wikibooks.org/wiki/Cascading_Style_Sheets/Important 摘自http://en.wikibooks.org/wiki/Cascading_Style_Sheets/重要

The important keyword makes a declaration take precedence over normal declarations—those that are not tagged with the important keyword. 重要关键字使声明优先于普通声明,即那些没有使用重要关键字标记的声明。 So "p { color: red ! important }" takes precedence over "p { color: green }". 因此,“ p {颜色:红色!重要}”优先于“ p {颜色:绿色}”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM