具有内联模型表单或表单集的基于 django 类的视图

发布于 2024-10-08 06:39:26 字数 670 浏览 4 评论 0原文

我有以下模型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

半窗疏影 2024-10-15 06:39:26

要点是:

  1. 使用 inlineformset_factoryforms.py 中生成 FormSet

    BookImageFormSet = inlineformset_factory(BookForm, BookImage, extra=2)
    BookPageFormSet = inlineformset_factory(BookForm, BookPage, extra=5)
    
  2. 返回FormSetviews.pyCreateView 类中的 code>:

    def get_context_data(self, **kwargs):
        context = super(BookCreateView, self).get_context_data(**kwargs)
        如果是 self.request.POST:
            context['bookimage_form'] = BookImageFormSet(self.request.POST)
            context['bookpage_form'] = BookPageFormSet(self.request.POST)
        别的:
            上下文['bookimage_form'] = BookImageFormSet()
            context['bookpage_form'] = BookPageFormSet()
        返回上下文
    
  3. 使用了 form_valid 保存表单和表单集:

     def form_valid(self, form):
         上下文 = self.get_context_data()
         bookimage_form = 上下文['bookimage_formset']
         bookpage_form = 上下文['bookpage_formset']
         如果 bookimage_form.is_valid() 和 bookpage_form.is_valid():
             self.object = form.save()
             bookimage_form.instance = self.object
             bookimage_form.save()
             bookpage_form.instance = self.object
             bookpage_form.save()
             返回 HttpResponseRedirect('谢谢/')
         别的:
             返回 self.render_to_response(self.get_context_data(form=form))
    

Key points is:

  1. generated FormSets within forms.py using inlineformset_factory:

    BookImageFormSet = inlineformset_factory(BookForm, BookImage, extra=2)
    BookPageFormSet = inlineformset_factory(BookForm, BookPage, extra=5)
    
  2. returned the FormSets within a CreateView class in views.py:

    def get_context_data(self, **kwargs):
        context = super(BookCreateView, self).get_context_data(**kwargs)
        if self.request.POST:
            context['bookimage_form'] = BookImageFormSet(self.request.POST)
            context['bookpage_form'] = BookPageFormSet(self.request.POST)
        else:
            context['bookimage_form'] = BookImageFormSet()
            context['bookpage_form'] = BookPageFormSet()
        return context
    
  3. Used form_valid to save the form and formset:

     def form_valid(self, form):
         context = self.get_context_data()
         bookimage_form = context['bookimage_formset']
         bookpage_form = context['bookpage_formset']
         if bookimage_form.is_valid() and bookpage_form.is_valid():
             self.object = form.save()
             bookimage_form.instance = self.object
             bookimage_form.save()
             bookpage_form.instance = self.object
             bookpage_form.save()
             return HttpResponseRedirect('thanks/')
         else:
             return self.render_to_response(self.get_context_data(form=form))
    
許願樹丅啲祈禱 2024-10-15 06:39:26

在检查了一些预制的 CBV 后,我刚刚添加了自己的版本。我特别需要控制多个表单集 ->单个视图中的一个父级,每个视图都有单独的保存功能。

我基本上将 FormSet 数据绑定填充到由 get_context_dataform_valid 调用的 get_named_formsets 函数中。

在那里,我检查所有表单集是否有效,并且还查找一种方法,该方法可以在每个表单集的基础上重写普通的旧 formset.save() 以进行自定义保存。

该模板通过

{% with named_formsets.my_specific_formset as formset %}
 {{ formset }}
 {{ formset.management_form }}
{% endwith %}

我想我会定期使用这个系统来呈现表单集。

class MyView(UpdateView): # FormView, CreateView, etc
    def get_context_data(self, **kwargs):
        ctx = super(MyView, self).get_context_data(**kwargs)
        ctx['named_formsets'] = self.get_named_formsets()
        return ctx

    def get_named_formsets(self):
        return {
            'followup': FollowUpFormSet(self.request.POST or None, prefix='followup'),
            'action': ActionFormSet(self.request.POST or None, prefix='action'),
        }

    def form_valid(self, form):
        named_formsets = self.get_named_formsets()
        if not all((x.is_valid() for x in named_formsets.values())):
            return self.render_to_response(self.get_context_data(form=form))

        self.object = form.save()

        # for every formset, attempt to find a specific formset save function
        # otherwise, just save.
        for name, formset in named_formsets.items():
            formset_save_func = getattr(self, 'formset_{0}_valid'.format(name), None)
            if formset_save_func is not None:
                formset_save_func(formset)
            else:
                formset.save()
        return http.HttpResponseRedirect('')

    def formset_followup_valid(self, formset):
        """
        Hook for custom formset saving.. useful if you have multiple formsets
        """
        followups = formset.save(commit=False) # self.save_formset(formset, contact)
        for followup in followups:
            followup.who = self.request.user
            followup.contact = self.object
            followup.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 by get_context_data and form_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

{% with named_formsets.my_specific_formset as formset %}
 {{ formset }}
 {{ formset.management_form }}
{% endwith %}

I think I'll be using this system regularly.

class MyView(UpdateView): # FormView, CreateView, etc
    def get_context_data(self, **kwargs):
        ctx = super(MyView, self).get_context_data(**kwargs)
        ctx['named_formsets'] = self.get_named_formsets()
        return ctx

    def get_named_formsets(self):
        return {
            'followup': FollowUpFormSet(self.request.POST or None, prefix='followup'),
            'action': ActionFormSet(self.request.POST or None, prefix='action'),
        }

    def form_valid(self, form):
        named_formsets = self.get_named_formsets()
        if not all((x.is_valid() for x in named_formsets.values())):
            return self.render_to_response(self.get_context_data(form=form))

        self.object = form.save()

        # for every formset, attempt to find a specific formset save function
        # otherwise, just save.
        for name, formset in named_formsets.items():
            formset_save_func = getattr(self, 'formset_{0}_valid'.format(name), None)
            if formset_save_func is not None:
                formset_save_func(formset)
            else:
                formset.save()
        return http.HttpResponseRedirect('')

    def formset_followup_valid(self, formset):
        """
        Hook for custom formset saving.. useful if you have multiple formsets
        """
        followups = formset.save(commit=False) # self.save_formset(formset, contact)
        for followup in followups:
            followup.who = self.request.user
            followup.contact = self.object
            followup.save()
饮惑 2024-10-15 06:39:26

您应该尝试 django-extra-views。查找 CreateWithInlinesView 和 UpdateWithInlinesView。

You should try out django-extra-views. Look for CreateWithInlinesView and UpdateWithInlinesView.

不离久伴 2024-10-15 06:39:26

我对原始解决方案做了一些修改,让 formset.is_valid() 工作:

    if self.request.POST:
        context['fs'] = MyInlineFS(self.request.POST, instance=self.object)
    else:
        context['fs'] = MyInlineFS(instance=self.object)

I made some modification to original solution to let formset.is_valid() to work:

    if self.request.POST:
        context['fs'] = MyInlineFS(self.request.POST, instance=self.object)
    else:
        context['fs'] = MyInlineFS(instance=self.object)
人疚 2024-10-15 06:39:26

我红色了 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.

月亮坠入山谷 2024-10-15 06:39:26

乔丹答案中的代码对我不起作用。我发布了关于此的我自己的问题,我相信我现在已经解决了。 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.

唱一曲作罢 2024-10-15 06:39:26

我需要对 Jordan 和 Speq 视图的 get_context_data() 进行另一项修改,以使 formset.non_form_errors 存在于模板上下文中。

...
if self.request.POST:
    context['fs'] = MyInlineFS(self.request.POST, instance=self.object)
    context['fs'].full_clean()  # <-- new
else:
    context['fs'] = MyInlineFS(instance=self.object)
return context

I needed to make one more modification to Jordan's and Speq's view's get_context_data() in order to have formset.non_form_errors exist in the template context.

...
if self.request.POST:
    context['fs'] = MyInlineFS(self.request.POST, instance=self.object)
    context['fs'].full_clean()  # <-- new
else:
    context['fs'] = MyInlineFS(instance=self.object)
return context
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文