【发布时间】:2015-08-07 12:20:49
【问题描述】:
我正在尝试在 Django 中创建一个自定义表单字段,以允许用户链接文件或上传文件。为此,我正在创建 MultiValueField 的子类,其中 fields 属性设置为 (URLField(), FileField())。我不确定这是不是正确的方法,但我不断收到一个我无法理解的错误:
'MyFileField' object has no attribute 'attrs'
这是我的代码。谁能解释发生了什么?
from django import forms
from django.core.exceptions import ValidationError
from .models import Case, Person, Opp, Category
class MyFileField(forms.MultiValueField):
def compress(self, data_list):
# I'll get to this once the attr error goes away
pass
def __init__(self, *args, **kwargs):
fields = (
forms.URLField(), forms.FileField()
)
super(MyFileField, self).__init__(fields=fields, *args, **kwargs)
class CaseForm(forms.ModelForm):
class Meta:
model = Case
fields = ['title', 'file', 'categories']
widgets = {
'file': MyFileField
}
【问题讨论】:
-
你不应该在这一行定义 attrs 吗?
file': MyFileField例如file': MyFileField(attrs={.....}) -
当我这样做时,我得到了错误:__init__() got an unexpected keyword argument 'attrs'
标签: django django-forms