根据 Django 文档,尝试 formfield_overrides 更改 TextField 字段大小时出现错误
尝试遵循文档中的以下说明: http://docs.djangoproject。 com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_overrides
收到此错误消息:“模块”对象没有属性“TextField”
我缺少什么?
models.py 中的此代码
class MyTest(models.Model):
mydesc = models.TextField(_('my desc'), )
admin.py 中的此代码(注意相关导入)
from django.contrib import admin
from django.db import models
from django.forms import Textarea
from myproject.myapp import MyTest
class MySizedTextarea(forms.Textarea):
def __init__(self, *args, **kwargs):
attrs = kwargs.setdefault('attrs', {})
attrs.setdefault('cols', 80)
attrs.setdefault('rows', 1)
super(MySizedTextarea, self).__init__(*args, **kwargs)
class MyTestAdmin(admin.ModelAdmin):
fields = ('mydesc', )
formfield_overrides = { models.TextField: {'widget': MySizedTextarea}, }
Trying to follow these directions in documentation:
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_overrides
Getting this error msg: 'module' object has no attribute 'TextField'
What am I missing?
This code in models.py
class MyTest(models.Model):
mydesc = models.TextField(_('my desc'), )
This code in admin.py (noting relevant imports)
from django.contrib import admin
from django.db import models
from django.forms import Textarea
from myproject.myapp import MyTest
class MySizedTextarea(forms.Textarea):
def __init__(self, *args, **kwargs):
attrs = kwargs.setdefault('attrs', {})
attrs.setdefault('cols', 80)
attrs.setdefault('rows', 1)
super(MySizedTextarea, self).__init__(*args, **kwargs)
class MyTestAdmin(admin.ModelAdmin):
fields = ('mydesc', )
formfield_overrides = { models.TextField: {'widget': MySizedTextarea}, }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试更明确地指定模型的命名空间(例如,如果使用 GIS 模型):
Try specifying the namespace for models more explicitly (for example, if using the GIS models):