inlineformset_factory创建新对象并在创建后编辑对象 [英] inlineformset_factory create new objects and edit objects after created

查看:146
本文介绍了inlineformset_factory创建新对象并在创建后编辑对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在django文档中,有一个使用inlineformset_factory编辑已创建对象的示例

In the django docs, there's an example of using inlineformset_factory to edit already created objects

https://docs.djangoproject.com/en/dev / topics / forms / modelforms /#using-an-inline-formset-in-a-view

我以这种方式改变了例子: / p>

I changed the example to be this way:

def manage_books(request):
    author = Author()
    BookInlineFormSet = inlineformset_factory(Author, Book, fields=('title',))
    if request.method == "POST":
        formset = BookInlineFormSet(request.POST, request.FILES, instance=author)
        if formset.is_valid():
            formset.save()
            return HttpResponseRedirect(author.get_absolute_url())
    else:
        formset = BookInlineFormSet(instance=author)
    return render_to_response("manage_books.html", {
        "formset": formset,
    })

使用上述,它只渲染内联模型,

With the above, it renders only the inline model without the parent model.

要创建一个新的对象,比如说作者,与多个图书相关联,使用inlineformset_factory,这是什么方法?

To create a new object, say Author, with multiple Books associated to, using inlineformset_factory, what's the approach?

使用django文档中的上述作者Book模型的一个例子将是有帮助的。 django文档仅提供了如何使用inlineformset_factory 编辑已创建对象的示例,但不提供创建新的对象

An example using the above Author Book model from django docs will be helpful. The django docs only provided example of how to edit already created object using inlineformset_factory but not to create new one

推荐答案

我没有正确地阅读你的问题。您还需要渲染父模型的表单。我没有测试过,我要离开我以前做过的和之前链接的答案,但它应该可以工作。

I didn't read your question properly at first. You need to also render the the form for the parent model. I haven't tested this, I'm going off what I've done before and the previously linked answer, but it should work.

更新

如果您正在使用视图进行编辑,则应首先检查作者ID。如果没有ID,它会将两个表单都渲染为一个新的实例,而使用ID将会填充现有的数据。然后你可以检查是否有POST请求。

If you're using the view to both and edit, you should check for an Author ID first. If there's no ID, it'll render both forms as a new instance, whereas with an ID it'll, fill them with the existing data. Then you can check if there was a POST request.

def manage_books(request, id):

    if id:
        author = Author.objects.get(pk=author_id)  # if this is an edit form, replace the author instance with the existing one
    else: 
        author = Author()
    author_form = AuthorModelForm(instance=author) # setup a form for the parent

    BookInlineFormSet = inlineformset_factory(Author, Book, fields=('title',))
    formset = BookInlineFormSet(instance=author)

    if request.method == "POST":
        author_form = AuthorModelForm(request.POST)

        if id: 
            author_form = AuthorModelForm(request.POST, instance=author)

        formset = BookInlineFormSet(request.POST, request.FILES)

        if author_form.is_valid():
            created_author = author_form.save(commit=False)
            formset = BookInlineFormSet(request.POST, request.FILES, instance=created_author)

            if formset.is_valid():
                created_author.save()
                formset.save()
                return HttpResponseRedirect(created_author.get_absolute_url())

    return render_to_response("manage_books.html", {
        "author_form": author_form,
        "formset": formset,
    })

这篇关于inlineformset_factory创建新对象并在创建后编辑对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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