英文:
Migrate Custom Field Renderer from Django 1.8 to Django 3.2
问题
我正在尝试将Django 1.8表单中的自定义字段渲染器迁移到Django 3.2。
该表单如下所示:
class SomeEditForm(forms.ModelForm):
job_uuid=forms.CharField(widget=forms.HiddenInput())
class Meta:
model=Job
fields=('status',)
widgets={'status': forms.RadioSelect(renderer=BSRadioFieldRenderer)}
看起来似乎不再有Rendermixin
,它不再在def __init__()
中接受渲染器。我已经阅读了新的源代码,看到RadioSelect
现在是ChoiceWidget
的子类。我无法弄清楚如何将我的旧渲染器嵌入到新结构中。
有人能指导我正确的方向吗?
感谢您的帮助!
英文:
I am trying to migrate a custom field renderer in a Django 1.8 form to Django 3.2.
The form looks like this:
class SomeEditForm(forms.ModelForm):
job_uuid=forms.CharField(widget=forms.HiddenInput())
class Meta:
model=Job
fields=('status',)
widgets={'status': forms.RadioSelect(renderer=BSRadioFieldRenderer)}
So as it seems there is no Rendermixin anymore which accepts a renderer in its def init(). I have read the new source code and see that RadioSelect is now a subclass of ChoiceWidget. I cant get my head around to inject my old renderer into the new structure.
Can someone please point me into the right direction?
Thanks for your help!
答案1
得分: 0
这似乎有一种可行的方法...
只需继承 forms.RadioSelect 并接受像这样的 renderer 参数:
class CustomRadioSelect(forms.RadioSelect):
def __init__(self, renderer=None, *args, **kwargs):
super(forms.RadioSelect, self).__init__(*args, **kwargs)
class SomeEditForm(forms.ModelForm):
job_uuid = forms.CharField(widget=forms.HiddenInput())
class Meta:
model = Job
fields = ('status',)
widgets = {'status': CustomRadioSelect(renderer=BSRadioFieldRenderer)}
请注意,这些类已经被截断,我只在这里写入了与我遇到的问题相关的部分。
英文:
There seems to be a hacky way which works...
Just subclass forms.RadioSelect and accept the renderer kwarg like this:
class CustomRadioSelect(forms.RadioSelect):
def __init__(self, renderer=None, *args, **kwargs):
super(forms.RadioSelect, self).__init__(*args, **kwargs)
class SomeEditForm(forms.ModelForm):
job_uuid=forms.CharField(widget=forms.HiddenInput())
class Meta:
model=Job
fields=('status',)
widgets = {'status': CustomRadioSelect(renderer=BSRadioFieldRenderer)}
Please note that these classes are truncated and I only wrote those parts in here which are relevant to the problem I had.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论