Django自定义字段:“module”对象没有属性“NameOfCustomField”

2024-09-23 22:26:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要使用Django表单中名为CustomModelChoiceField的自定义字段的帮助。我试图重写ModelChoiceField的label_from_instance方法

我的表格:

from django import forms
from myfolder.otherfolder.fields import CustomModelChoiceField

class MyForm(forms.Form):
    # I had to override init to get some paramaters, so I'll simplify it
    def __init__(self, *args, **kwargs):
        #skipped querying code
        self.fields['custom'] = CustomModelChoiceField(queryset = custom_dates,
                                                      required = False,
                                                      widget = forms.Select(attrs={"onChange":'changeCalendar()'}),
                                                      label = "Choose a date")

我的习惯字段.py文件(与表单.py,并且import语句起作用)。取自docs

^{pr2}$

当我试图查看页面时,我得到:

'module' object has no attribute 'CustomModelChoiceField'

可能是我的字段.py在错误的文件夹里或者我在子类化方面很差劲。有什么想法吗?在


Tags: todjangoinstancefrompyimportself表单