具有内联模型表单或表单集的基于 django 类的视图
我有以下模型:
class Bill(models.Model):
date = models.DateTimeField(_("Date of bill"),null=True,blank=True)
class Item(models.Model):
name = models.CharField(_("Name"),max_length=100)
price = models.FloatField(_("Price"))
quantity = models.IntegerField(_("Quantity"))
bill = models.ForeignKey("Bill",verbose_name=_("Bill"),
related_name="billitem")
我知道这是可能的:
from django.forms.models import inlineformset_factory
inlineformset_factory(Bill, Item)
然后通过标准视图处理它。
现在我想知道,是否有一种方法可以使用基于类的视图(不适用于管理界面)来实现相同的目的(意思是:使用内联来添加/编辑属于帐单的项目)。
I have the following models:
class Bill(models.Model):
date = models.DateTimeField(_("Date of bill"),null=True,blank=True)
class Item(models.Model):
name = models.CharField(_("Name"),max_length=100)
price = models.FloatField(_("Price"))
quantity = models.IntegerField(_("Quantity"))
bill = models.ForeignKey("Bill",verbose_name=_("Bill"),
related_name="billitem")
I know that this is possible:
from django.forms.models import inlineformset_factory
inlineformset_factory(Bill, Item)
and then process this via standard view.
Now I was wondering, if there is a way to achieve the same (meaning: using a inline for adding/editing items belonging to a bill) using class based views (not for the admin-interface).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
要点是:
使用
inlineformset_factory
在forms.py
中生成FormSet
:返回
FormSetviews.py
中CreateView
类中的 code>:使用了
form_valid
保存表单和表单集:Key points is:
generated
FormSet
s withinforms.py
usinginlineformset_factory
:returned the
FormSet
s within aCreateView
class inviews.py
:Used
form_valid
to save the form and formset:在检查了一些预制的 CBV 后,我刚刚添加了自己的版本。我特别需要控制
多个表单集 ->单个视图中的一个父级
,每个视图都有单独的保存功能。我基本上将 FormSet 数据绑定填充到由
get_context_data
和form_valid
调用的get_named_formsets
函数中。在那里,我检查所有表单集是否有效,并且还查找一种方法,该方法可以在每个表单集的基础上重写普通的旧
formset.save()
以进行自定义保存。该模板通过
我想我会定期使用这个系统来呈现表单集。
I just added my own version after checking out some of those pre-made CBVs. I specifically needed control over
multiple formsets -> one parent
in a single view each with individual save functions.I basically stuffed the FormSet data binding into a
get_named_formsets
function which is called byget_context_data
andform_valid
.There, I check if all formsets are valid, and also look for a method that overrides a plain old
formset.save()
on a per formset basis for custom saving.The template renders formsets via
I think I'll be using this system regularly.
您应该尝试 django-extra-views。查找 CreateWithInlinesView 和 UpdateWithInlinesView。
You should try out django-extra-views. Look for
CreateWithInlinesView
andUpdateWithInlinesView
.我对原始解决方案做了一些修改,让 formset.is_valid() 工作:
I made some modification to original solution to let formset.is_valid() to work:
我红色了 1.3-beta-1 的通用源代码:
代码绝对没有准备好进行列表编辑,或者这里有一些黑魔法。
但我认为它可以很快实施。
如果您查看 django.view.generic.edit (支持详细对象编辑)模块,它如何使用 django.view.generic.detail 模块。
我认为 django.view.generic.list_edit 模块可以使用 django.view.generic.list 和 django.view.generic.edit 的某些部分来实现。
I red the generic source code of the 1.3-beta-1 :
The code is absolutely not ready for List editing or there is some black magic here.
But I think that it can be implemented quickly.
If you look at the django.view.generic.edit (that support detailed object editing) module how it use the django.view.generic.detail module.
I think that a django.view.generic.list_edit module can be implemented using django.view.generic.list and some part from django.view.generic.edit.
乔丹答案中的代码对我不起作用。我发布了关于此的我自己的问题,我相信我现在已经解决了。 inlineformset_factory 的第一个参数应该是 Book,而不是 BookForm。
The code in Jordan's answer didn't work for me. I posted my own question about this, which I believe I've now solved. The first argument to inlineformset_factory should be Book, not BookForm.
我需要对 Jordan 和 Speq 视图的
get_context_data()
进行另一项修改,以使formset.non_form_errors
存在于模板上下文中。I needed to make one more modification to Jordan's and Speq's view's
get_context_data()
in order to haveformset.non_form_errors
exist in the template context.