有没有人设法在django-fobi中设置默认处理程序?方法是什么?我想将db_store处理程序设置为所有表单的默认值。
我试图在模型中设置默认值但没有成功。
没有开箱即用的解决方案。
但是,您可以执行以下操作:
解决方案1:
使用django信号,观察FormEntry
模型的更新,并在每次保存表单时以编程方式添加db_handler插件(它尚未分配)。
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
@receiver(post_save)
def update_document(sender, **kwargs):
app_label = sender._meta.app_label
model_name = sender._meta.model_name.lower()
instance = kwargs['instance']
if app_label == 'fobi' and model_name == 'formentry':
from fobi.models import FormHandlerEntry
FormHandlerEntry.objects.get_or_create(
plugin_uid='db_store',
form_entry=instance
)
解决方案2:
您还可以注册表单回调(fobi几乎在表单提交过程的每个阶段都实现了回调)。
在你的回调中,你必须模仿db_store
插件的功能(主要是复制粘贴)。
import datetime
import simplejson as json
from fobi.base import (
form_callback_registry,
FormCallback,
get_processed_form_data,
)
from fobi.constants import CALLBACK_FORM_VALID
from fobi.contrib.plugins.form_handlers.db_store.models import SavedFormDataEntry
class AutoDbStore(FormCallback):
stage = CALLBACK_FORM_VALID
def callback(self, form_entry, request, form):
form_element_entries = form_entry.formelemententry_set.all()
# Clean up the values, leave our content fields and empty values.
field_name_to_label_map, cleaned_data = get_processed_form_data(
form,
form_element_entries
)
for key, value in cleaned_data.items():
if isinstance(value, (datetime.datetime, datetime.date)):
cleaned_data[key] = value.isoformat() \
if hasattr(value, 'isoformat') \
else value
saved_form_data_entry = SavedFormDataEntry(
form_entry=form_entry,
user=request.user if request.user and request.user.pk else None,
form_data_headers=json.dumps(field_name_to_label_map),
saved_data=json.dumps(cleaned_data)
)
saved_form_data_entry.save()
form_callback_registry.register(AutoDbStore)