如何使用通用UpdateView的自定义窗口小部件,而无需重新定义整个窗体? [英] How does one use a custom widget with a generic UpdateView without having to redefine the entire form?

查看:120
本文介绍了如何使用通用UpdateView的自定义窗口小部件,而无需重新定义整个窗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有ManyToMany关系的模型,我想用一个CheckBoxSelectMultiple小部件更新,而其他所有内容都使用默认的通用形式,但是当我重新定义一个表单字段时,它是唯一显示在UpdateView中的表单。有没有办法使用只有一个字段的小部件,而无需重新定义整个表单?



Views.py:


$ b $来自django.views.generic.edit导入的更新视图

从kunden.models导入Kunde,Unternehmenstyp
从kunden.forms导入KundeEditForm

class KundeUpdate(UpdateView):
model = Kunde
form_class = KundeEditForm
template_name ='kunden / kunde_update.html'
success_url ='/'

forms.py:

来自django.forms.widgets导入的导入CheckboxSelectMultiple 
from django.forms import ModelMultipleChoiceField,ModelForm

from kunden.models import Kunde,Unternehmenstyp

类KundeEditForm(ModelForm):
model = Kunde
unternehmenstyp = ModelMultipleChoiceField(widget = CheckboxSelectMultiple,required = True,queryset = Unternehmenstyp.objects.all())

我知道这有ap一个简单的解决方案,所以谢谢你的耐心提前大家。



虽然我可以任何人推荐任何django书籍值得阅读?我已经通过基础教程,根据需要挖掘文档,并阅读了Django的两个Scoops: https://django.2scoops.org/ 所以如果你能想到一本书在我的一级的人,那将是不胜感激。再次感谢

解决方案

尝试使用 class Meta

从django.forms.widgets导入CheckboxSelectMultiple 
从django.forms导入ModelMultipleChoiceField,ModelForm

from kunden.models import Kunde,Unternehmenstyp

class KundeEditForm(ModelForm):
class Meta:#model必须在Meta类中
model = Kunde
unternehmenstyp = ModelMultipleChoiceField (widget = CheckboxSelectMultiple,required = True,queryset = Unternehmenstyp.objects.all())

REF: https://docs.djangoproject.com/en/1.5/topics/forms/modelforms/# modelform



如果您只需要简单的覆盖,您也可以使用modelform工厂:



<$从django.views.generic.edit导入UpdateView
从django.forms.models导入modelform_fact p $ p>

从kunden.models导入Kunde,Unternehmenstyp

class KundeUpdate(UpdateView):
model = Kunde
form_class = modelform_factory(Kunde,
widgets = {unternehmenstyp:CheckboxSelectMultiple})
template_name ='kunden / kunde_update.html'
success_url ='/'

参考: https: //docs.djangoproject.com/en/1.5/topics/forms/modelforms/#modelform-factory-function


I have a model with a ManyToMany relation that I would like to update with a CheckBoxSelectMultiple widget while everything else uses the default generic form, but when I redefine that one form field, it is the only one that shows up in the UpdateView. Is there a way to use a widget with just one field without having to redefine the entire form?

Views.py:

from django.views.generic.edit import UpdateView

from kunden.models import Kunde, Unternehmenstyp
from kunden.forms import KundeEditForm

class KundeUpdate(UpdateView):
    model = Kunde
    form_class =  KundeEditForm
    template_name = 'kunden/kunde_update.html'
    success_url = '/'

forms.py:

from django.forms.widgets import CheckboxSelectMultiple
from django.forms import ModelMultipleChoiceField,ModelForm

from kunden.models import Kunde, Unternehmenstyp

class KundeEditForm(ModelForm):
    model = Kunde
    unternehmenstyp = ModelMultipleChoiceField(widget=CheckboxSelectMultiple,required=True, queryset=Unternehmenstyp.objects.all())

I know this has a painfully simple solution, so thanks for your patience in advance everyone.

While I'm at it can anyone recommend any django books worth reading? I've gone through the base tutorial, dug through documentation as needed, and read Two Scoops of Django: https://django.2scoops.org/ so if you can think of a book for someone at my level, that'd be greatly appreciated. thanks again

解决方案

Try this, with class Meta:

from django.forms.widgets import CheckboxSelectMultiple
from django.forms import ModelMultipleChoiceField,ModelForm

from kunden.models import Kunde, Unternehmenstyp

class KundeEditForm(ModelForm):
    class Meta: # model must be in the Meta class
        model = Kunde
    unternehmenstyp = ModelMultipleChoiceField(widget=CheckboxSelectMultiple,required=True, queryset=Unternehmenstyp.objects.all())

REF: https://docs.djangoproject.com/en/1.5/topics/forms/modelforms/#modelform

You can also use modelform factories if you only need to make a simple override:

from django.views.generic.edit import UpdateView
from django.forms.models import modelform_factory

from kunden.models import Kunde, Unternehmenstyp

class KundeUpdate(UpdateView):
    model = Kunde
    form_class =  modelform_factory(Kunde,
        widgets={"unternehmenstyp": CheckboxSelectMultiple })
    template_name = 'kunden/kunde_update.html'
    success_url = '/'

REF: https://docs.djangoproject.com/en/1.5/topics/forms/modelforms/#modelform-factory-function

这篇关于如何使用通用UpdateView的自定义窗口小部件,而无需重新定义整个窗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆