开发者

Getting error trying out formfield_overrides to alter TextField field size, per Django documentation

开发者 https://www.devze.com 2023-02-16 15:35 出处:网络
Trying to follow these directions in documentation: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_overrides

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):  
  开发者_JS百科  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}, }


Try specifying the namespace for models more explicitly (for example, if using the GIS models):

import django.contrib.gis.db.models


class MyAdmin(admin.ModelAdmin):
    formfield_overrides = { django.contrib.gis.db.models.TextField: {'widget': forms.TextInput(attrs={'size':20})},}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号