在自动完成灯的早期版本中,有一种非常快速的方式来添加新表单。
form = modelform_factory(ModelName, fields='__all__')
如果模型已注册自动完成视图,则会根据给定的模型自动构建新表单。非常快速和容易。在版本3.1.6,即今天发布的最新版本中,此功能似乎已被删除。我不得不返回并重新修改所有内容以使我们升级,并且我想知道是否有新版本中我可能错过的旧版modelform_factory
。或者是否有一种快速设置可以轻松重复使用的通用自动完成视图/表单的方法?任何想法都表示赞赏。
答案 0 :(得分:1)
因此经过大量的狩猎后,我无法找到类似于之前自动完成灯的modelform_factory的内容,所以我决定自己制作。以下是我们在生产CMS中直接采用的示例。
from dal import autocomplete
from mymodels import ThisModel, ThatModel, AnotherModel
def autocomplete_form_factory(ac_model, *args, **kwargs):
field_url_dict = {}
m2m = ('mypeeps', 'yourpeeps',)
if ac_model in (ThisModel, ThatModel):
# Connects the "stuff_field" of these
# models to the url named "stuff-autocomplete"
field_url_dict = {
'stuff_field': 'stuff'
}
elif ac_model == AnotherModel:
# Connects AnotherModel's ForeignKey field "headhoncho"
# and its ManyToManyFields "mypeeps" and "yourpeeps"
# to the "peeps-autocomplete" url
field_url_dict = {
'headhoncho': 'peeps',
'mypeeps': 'peeps',
'yourpeeps': 'peeps',
}
# Assign the appropriate widgets based on this model's autocomplete dictionary
ac_widgets = {}
ac_fields = kwargs.get('fields', ('__all__'))
for field, url in field_url_dict.iteritems():
is_m2m = field in m2m
text = 'Type to return a list of %s...' if is_m2m else 'Type to return a %s list...'
kwargs = {
'url': '%s-autocomplete' % url,
'attrs': {
'data-placeholder': text % ac_model._meta.get_field(field).verbose_name,
'data-minimum-input-length': 3,
}
}
ac_widgets[field] = autocomplete.ModelSelect2Multiple(**kwargs) if is_m2m else autocomplete.ModelSelect2(**kwargs)
# Create the form
class DynamicAutocompleteForm(forms.ModelForm):
class Meta:
model = ac_model
fields = ac_fields
widgets = ac_widgets
return DynamicAutocompleteForm
对于相应的视图,您可以执行以下操作:
class BaseAutocomplete(autocomplete.Select2QuerySetView):
model = None
fields = ['title']
filters = {}
def get_queryset(self):
if not self.request.user.is_authenticated() or not self.q or len(self.q) < 3:
return self.model.objects.none()
# OR all of our field searches together
obj = Q()
for field in self.fields:
kwargs = {}
kwargs['%s__icontains' % field] = self.q
obj = obj | Q(**kwargs)
return self.model.objects.filter(obj).filter(**self.filters)
class StuffAutocomplete(BaseAutocomplete):
model = Stuff
filters = {'is_awesome': True}
class PeepsAutocomplete(BaseAutocomplete):
model = Peeps
fields = ['name', 'notes']
对于网址,您可以使用以下内容:
url(r'^stuff-autocomplete/$', StuffAutocomplete.as_view(), name='stuff-autocomplete'),
url(r'^peeps-autocomplete/$', PeepsAutocomplete.as_view(), name='peeps-autocomplete',),
要在代码中使用表单工厂,您可以使用以下内容,将结果分配给模型管理员表单或将其用作更复杂表单的基础:
autocomplete_form_factory(ThisModel)
autocomplete_form_factory(AnotherModel, fields=["headhoncho", "mypeeps"])
这模仿了我们对autocomplete早期的modelform_factory所具有的功能,使得创建自动完成表单变得非常简单,并在设置好所有内容后根据需要将它们添加到管理员。希望这有助于节省一些时间。