Django表单集合----Formset
概述:Formset(表单集)是多个表单的集合。Formset在Web开发中应用很普遍,它可以让用户在同一个页面上提交多张表单,一键添加多个数据,比如一个页面上添加多个用户信息,下面将会详细讲述如何使用Formset。
一、Formset的分类
Django针对不同的formset提供了三种方法:formset_factory,modelformset_factory和inlineformset_factory。
二、如何使用formset_factory
对于继承forms.Form的自定义表单,我们可以使用formset_factory。
#models.py from django import forms class BookForm(forms.Form):
name = forms.CharField(max_length=100)
title = forms.CharField()
pub_date = forms.DateField(required=False) # forms.py - build a formset of books from django.forms import formset_factory
from myapp.models import BookForm # extra: 想要显示空表单的数量
# max_num: 表单显示最大数量,可选,默认1000 BookFormSet = formset_factory(BookForm, extra=3, max_num=2)
在视图文件views.py里,我们可以像使用form一样使用formset
# views.py - formsets example. from .forms import BookFormSet
from django.shortcuts import render def manage_books(request):
if request.method == 'POST':
formset = BookFormSet(request.POST)
if formset.is_valid():
# do something with the formset.cleaned_data
pass
else:
formset = BookFormSet()
#如果想传入初始数据可设置initial = [{'name':'python','pub_date':'北京出版社'}]
return render(request, 'manage_books.html', {'formset': formset})
注意:如果使用了 initial 来显示formset,那么您需要在处理formset提交时传入相同的 initial ,以便formset检测用户更改了哪些表单。例如,您可能有这样的: BookFormSet(request.POST, initial=[...])。
模板里可以这样使用formset:
<form action=”.” method=”POST”>
{{ formset }}
</form>
也可以这样使用:
<form method="post">
{{ formset.management_form }} #一定要加这行代码
<table>
{% for form in formset %}
{{ form }}
{% endfor %}
</table>
</form>
formset_factory()参数解释:
1、如果 max_num 的值大于初始数据现有数量,那空白表单可显示的数量取决于 extra 的数量,只要总表单数不超过 max_num 。例如, extra=2 , max_num=2 并且formset有一个 initial 初始化项,则会显示一张初始化表单和一张空白表单。
2、如果初始数据项的数量超过 max_num ,那么 max_num 的值会被无视,所有初始数据表单都会显示,并且也不会有额外的表单显示。例如,假设 extra=3 , max_num=1 并且formset有两个初始化项,那么只会显示两张有初始化数据的表单。
3、max_num 的值 None (默认值),它限制最多显示(1000)张表单,其实这相当于没有限制。
三、如何使用modelformset_factory
Formset也可以直接由模型model创建,这时你需要使用modelformset_factory。你可以指定需要显示的字段和表单数量。
class StudentStudyRecordModelForm(forms.ModelForm):
class Meta:
model=StudentStudyRecord
fields=["score","homework_note"]
由ModelForm创建formset:
model_formset_cls=modelformset_factory(model=StudentStudyRecord,form=StudentStudyRecordModelForm,extra=0)
views.py
class RecordScoreView(View):
def get(self, request,class_study_record_id):
model_formset_cls=modelformset_factory(model=StudentStudyRecord,form=StudentStudyRecordModelForm,extra=0)
queryset = StudentStudyRecord.objects.filter(classstudyrecord=class_study_record_id)
formset = model_formset_cls(queryset=queryset)
return render(request,"student/record_score.html",locals())
def post(self, request,class_study_record_id):
model_formset_cls = modelformset_factory(model=StudentStudyRecord, form=StudentStudyRecordModelForm, extra=0)
formset=model_formset_cls(request.POST)
if formset.is_valid():
formset.save()
return redirect(request.path)
模板:
<form method="post" action="">
{% csrf_token %}
{{ formset.management_form }}
<table class="table table-bordered">
<thead>
<tr>
<th>姓名</th>
<th>考勤</th>
<th>作业成绩</th>
<th>作业评语</th>
</tr>
</thead>
<tbody>
{% for form in formset %}
<tr>
{{ form.id }}
<td>{{ form.instance.student }}</td>
<td>{{ form.instance.get_record_display }} </td>
<td>{{ form.score }} </td>
<td>{{ form.homework_note }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<input type="submit" value="保存">
</form>
四、如何使用inlineformset_factory
试想我们有如下recipe(菜谱)模型,Recipe(菜谱)与Ingredient(原料)是一对多的关系。一般的formset只允许我们一次性提交多个Recipe或多个Ingredient。但如果我们希望同一个页面上添加一个菜谱(Recipe)和多个原料(Ingredient),这时我们就需要用使用inlineformset了。
from django.db import models class Recipe(models.Model):
title = models.CharField(max_length=255)
description = models.TextField() class Ingredient(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE, related_name='ingredient')
name = models.CharField(max_length=255)
利用inlineformset_factory创建formset的方法如下所示。该方法的第一个参数和第二个参数都是模型,其中第一个参数必需是ForeignKey。
# forms.py from django.forms import ModelForm
from django.forms import inlineformset_factory from .models import Recipe, Ingredient, Instruction class RecipeForm(ModelForm):
class Meta:
model = Recipe
fields = ("title", "description",) IngredientFormSet = inlineformset_factory(Recipe, Ingredient, fields=('name',),
extra=3, can_delete=False, max_num=5)
views.py中使用formset创建和更新recipe(菜谱)的代码如下。在对IngredientFormSet进行实例化的时候,必需指定recipe的实例。
def recipe_update(request, pk): #更新
recipe = get_object_or_404(Recipe, pk=pk)
if request.method == "POST":
form = RecipeForm(request.POST, instance=recipe) if form.is_valid():
recipe = form.save()
ingredient_formset = IngredientFormSet(request.POST, instance=recipe) if ingredient_formset.is_valid():
ingredient_formset.save() return redirect('/recipe/')
else:
form = RecipeForm(instance=recipe)
ingredient_formset = IngredientFormSet(instance=recipe) return render(request, 'recipe/recipe_update.html', {'form': form,
'ingredient_formset': ingredient_formset,
}) def recipe_add(request): #创建
if request.method == "POST":
form = RecipeForm(request.POST) if form.is_valid():
recipe = form.save()
ingredient_formset = IngredientFormSet(request.POST, instance=recipe) if ingredient_formset.is_valid():
ingredient_formset.save() return redirect('/recipe/')
else:
form = RecipeForm()
ingredient_formset = IngredientFormSet() return render(request, 'recipe/recipe_add.html', {'form': form,
'ingredient_formset': ingredient_formset,
})
模板recipe/recipe_add.html代码如下:
<h1>Add Recipe</h1>
<form action="." method="post">
{% csrf_token %}
{{ form.as_p }}
<fieldset>
<legend>Recipe Ingredient</legend>
{{ ingredient_formset.management_form }}
{{ ingredient_formset.non_form_errors }}
{% for form in ingredient_formset %}
{{ form.name.errors }}
{{ form.name.label_tag }}
{{ form.name }}
</div>
{% endfor %}
</fieldset> <input type="submit" value="Add recipe" class="submit" />
</form>
Django表单集合----Formset的更多相关文章
- python 全栈开发,Day111(客户管理之 编辑权限(二),Django表单集合Formset,ORM之limit_choices_to,构造家族结构)
昨日内容回顾 1. 权限系统的流程? 2. 权限的表有几个? 3. 技术点 中间件 session orm - 去重 - 去空 inclusion_tag filter 有序字典 settings配置 ...
- Django表单集合Formset的高级用法
Formset(表单集)是多个表单的集合.Formset在Web开发中应用很普遍,它可以让用户在同一个页面上提交多张表单,一键添加多个数据,比如一个页面上添加多个用户信息.今天小编我就介绍下Djang ...
- python3之Django表单(一)
1.HTML中的表单 在HTML种,表单是在<form>...</form>种的元素,它允许用户输入文本,选择选项,操作对象等,然后发送这些数据到服务器 表单元素允许用户在表单 ...
- 在一般处理程序中,把Form Post过来的表单集合转换成对象 ,仿 MVC post,反射原理
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.L ...
- django表单的api
django表单的api,参考文档:https://yiyibooks.cn/xx/Django_1.11.6/ref/forms/api.html 绑定与未绑定形式: Form要么是绑定的,要么是未 ...
- Django表单API详解
声明:以下的Form.表单等术语都指的的广义的Django表单. Form要么是绑定了数据的,要么是未绑定数据的. 如果是绑定的,那么它能够验证数据,并渲染表单及其数据,然后生成HTML表单.如果未绑 ...
- 9:django 表单
django自带表单系统,这个表单系统不仅可以定义属性名,还可以自己定义验证,更有自己自带的错误提示系统 这节我们仅仅粗略的来看一下django表单系统的入门运用(具体的实在太多东西,主要是我觉得有很 ...
- django 表单系统 之 forms.Form
继承forms.Form实现django表单系统 参考: https://www.cnblogs.com/zongfa/p/7709639.html https://www.cnblogs.com/c ...
- 关于创建Django表单Forms继承BaseForm的问题
在创建Django表单时,因为需要验证用户输入的验证码是否正确,因此需要在session里提取当前验证码的值和POST提交过来的值进行比对,如图: form.py from django import ...
随机推荐
- Hadoop源代码点滴-文件系统HDFS
HDFS是Hadoop集群的文件系统,这是一种分布(distributed).容错(fault tolerant)的文件系统 所谓分布,是说整个文件系统的内容并非集中存储在一台或几台“文件服务器上”, ...
- DocumentFormat.OpenXml read excel file
这里向大家介绍一种读取excel 数据的方法,用的是DoucmentFormat.OpenXml.dll 废话不多说,向大家展示一下在项目中处理过的方法,如果有任何疑问,随时联系我. using Do ...
- .net core 3.0 Signalr - 02 使用强类型的Hub
## 强类型的优缺点 - 优点 强类型的Hub可以避免魔法函数名,相比弱类型更容易维护和发现问题,直接上代码 - 缺点 特么的得多些好几行代码 ## 代码 ### 接口定义 ``` C# /// // ...
- css 添加手状样式,鼠标移上去变小手
cursor:pointer, 简单实用. 前端工作一年多,竟然没有博客.说出来别人都要笑话,这是一个新的开始.
- hover和position共用出现的问题
hover 鼠标移入的样式 position 定位属性 包含 relative 相对定位 absolute 绝对定位为 fixed 固定定位 hover作用范围 可以实现自己样式的 ...
- MySQL 深入理解索引B+树存储 (转载))
出处:http://blog.codinglabs.org/articles/theory-of-mysql-index.html 摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一 ...
- Spark 学习笔记之 Standalone与Yarn启动和运行时间测试
Standalone与Yarn启动和运行时间测试: 写一个简单的wordcount: 打包上传运行: Standalone启动: 运行时间: Yarn启动: 运行时间: 测试结果: Standalon ...
- Ansible Roles角色
Roles小技巧: 1.创建roles目录结构,手动或使用ansible-galaxy init test roles 2.编写roles的功能,也就是tasks. nginx rsyncd memc ...
- 手把手教你安装Eclipse最新版本的详细教程 - 大佬的鸡肋,菜鸟的盛宴(非常详细,非常实用)
简介 首先声明此篇文章主要是针对测试菜鸟或者刚刚入门的小伙们或者童鞋们,大佬就没有必要往下看了. 写这篇文章的由来是因为后边要用这个工具,但是由于某些原因有部分小伙伴和童鞋们可能不会安装此工具,为了方 ...
- C#常见的算法面试题
一.求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m //方法一,通过顺序规律写程序,同时也知道flag标志位的重要性. static int F1(int m) { in ...