python - Django:在没有 javascript 的情况下添加内联表单集行

标签 python django django-models django-forms django-views

这篇文章与此有关: Add row to inlines dynamically in django admin

有没有办法在不使用 javascript 的情况下添加内联表单集?显然,会涉及到页面刷新。

所以,如果表单有一个名为“添加”的按钮...

我想我可以这样做:

if request.method=='POST':
  if 'add' in request.POST:
    PrimaryFunctionFormSet = inlineformset_factory(Position,Function,extra=1)
    prims = PrimaryFunctionFormSet(request.POST)

我认为每次都会加 1,然后用发布数据填充表单。但是,extra=1好像并没有给post数据加1。

最佳答案

明白了。

有时这是最简单的解决方案。只需复制 request.POST 数据并修改 TOTAL-FORMS。

例如..

if request.method=='POST':
  PrimaryFunctionFormSet = inlineformset_factory(Position,Function)
  if 'add' in request.POST:
    cp = request.POST.copy()
    cp['prim-TOTAL_FORMS'] = int(cp['prim-TOTAL_FORMS'])+ 1
    prims = PrimaryFunctionFormSet(cp,prefix='prim')

然后像往常一样吐出表格。保留您的数据,添加内联编辑器。

关于python - Django:在没有 javascript 的情况下添加内联表单集行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2448970/

相关文章:

python - 在*没有* Internet 连接的情况下安装 MySQLDb python 模块

python - Django 从 id 中过滤多对多

django - 序列化程序嵌套关系无法正常工作

python -/django.db.utils.IntegrityError : NOT NULL constraint failed/after python manage. py 迁移应用程序零

python - 调用异步任务 celery 时引发异常 : "NameError: global name * is not defined"

python - 如何在 QTabWidget 中设置可移动选项卡的样式

python - 从 'BlobServiceClient' 导入 'azure.storage.blob' 时出错

python - Pandas 数据框 : Count unique words in a column and return count in another column

django - Django 自己的登录测试用例不遵循可定制的身份验证后端。为什么?

python - 保存多对多字段 Django 表单