django:为formset_factory表单元素设置默认值 [英] django : set default value for formset_factory form element

查看:477
本文介绍了django:为formset_factory表单元素设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模型:

  class AssociatedFileCourse(models.Model)
file_original = models.FileField(upload_to ='assets / associated_files')
session = models.ForeignKey(Session)
title = models.CharField(max_length = 500)

形式:

  class AddAssociatedFilesForm(ModelForm):
class Meta:
model = AssociatedFileCourse

如果我不得不从上面的定义创建一个单一的形式,并设置一些初始值,它可以使用初始参数,如



form = AddAssociatedFilesForm(initial = {'session':Session.objects.get(pk = id )创建一个formset_factory表单时,如何设置初始表单值如下:


$ b $ p $ b

  AddAssociatedFilesFormSet = formset_factory(AddAssociatedFilesForm)
form = AddAssociatedFilesFormSet()


解决方案

哟你想使用 modelformset_factory ,这是定制的创建并处理Model实例的formets。

  from django.forms.models import modelformset_factory 

#通过指定使用
的模型和表单来创建表单集合AsAssociatedFilesFormSet = modelformset_factory(AssociatedFileCourse,form = AddAssociatedFilesForm)

#由于您没有对自定义窗体执行任何特殊操作,所以
#你甚至不需要定义你自己的Form类
AddAssociatedFilesFormSet = modelformset_factory(AssociatedFileCourse)

默认情况下,Model表单将为每个Model实例(即 Model.objects.all()显示一个表单。此外,您将拥有允许您创建新的Model实例的空白表单。空格的数量受 max_num extra kwargs传递给modelformset_factory()。



如果您想要初始数据,您可以在生成表单集时使用初始 kwarg指定。注意,初始数据需要在列表中。

  formset = AddAssociatedFilesFormSet(queryset = AssociatedFileCourse.objects.none() 
initial = [{'session':Session.objects.get(pk = id)}])


$ b $这应该是你想要的。但是,您不能(至少在当前的Django版本中)使用现有的Model实例 extra 表单的初始数据创建一个Model formset。这就是为什么objects.none()queryset在那里。将它设置为objects.all()或删除 queryset kwarg,如果您有实例,额外的表单将不具有初始数据。



使用初始数据进一步阅读模型表单 - 请参阅此 post


Model:

class AssociatedFileCourse(models.Model)
  file_original = models.FileField(upload_to = 'assets/associated_files')
  session = models.ForeignKey(Session)
  title = models.CharField(max_length=500)

Form:

class AddAssociatedFilesForm(ModelForm):
  class Meta:
    model = AssociatedFileCourse

If I had to create a single form from above defination and set some initial value, It could have done using initial parameter like

form = AddAssociatedFilesForm(initial={'session': Session.objects.get(pk=id)})

how to set the initial form values when creating a formset_factory forms like:

AddAssociatedFilesFormSet = formset_factory(AddAssociatedFilesForm)
form = AddAssociatedFilesFormSet()

解决方案

You want to use modelformset_factory, which is tailored to create and act on formsets of Model instances.

from django.forms.models import modelformset_factory

# create the formset by specifying the Model and Form to use
AddAssociatedFilesFormSet = modelformset_factory(AssociatedFileCourse, form=AddAssociatedFilesForm)

# Because you aren't doing anything special with your custom form,
# you don't even need to define your own Form class
AddAssociatedFilesFormSet = modelformset_factory(AssociatedFileCourse)

By default a Model formset will display a form for every Model instance - i.e. Model.objects.all(). In addition you'll have blank forms allowing you to create new Model instances. The number of blank forms is subject to the max_num and extra kwargs passed to modelformset_factory().

If you want initial data you can specify it with the initial kwarg when generating the formset. Note, the initial data needs to be inside a list.

formset = AddAssociatedFilesFormSet(queryset=AssociatedFileCourse.objects.none(),
                                    initial=[{'session': Session.objects.get(pk=id)}])

That should look like you want it. However, you cannot (in current Django versions at least) create a Model formset with existing Model instances and initial data for the extra forms. That's why the objects.none() queryset is there. Set it to objects.all() or remove the queryset kwarg and - if you have instances - the extra forms will not have the initial data.

Further reading on Model formsets with initial data - see this post.

这篇关于django:为formset_factory表单元素设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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