在Django中如何得到内联表单的相反方式? [英] How do I get the reverse of the inline formset in Django?
问题描述
请参阅以下 Django的文档:
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(Author)
title = models.CharField(max_length=100)
如果要创建一个允许您可以编辑属于特定作者的书籍
,您可以这样做:
If you want to create a formset that allows you to edit books belonging to a particular author, you could do this:
>>> from django.forms.models import inlineformset_factory
>>> BookFormSet = inlineformset_factory(Author, Book)
>>> author = Author.objects.get(name=u'Mike Royko')
>>> formset = BookFormSet(instance=author)
与作者到书籍的许多关系,并提供了一个简单的方法来编辑一个作者的书籍。
This is a one-to-many relationship with Authors to Books and provides an easy way to edit the books for a single author.
现在我想做相反的:一个表单来编辑/创建具有书籍的内联格式的作者以编辑/创建。我怎么做?我真的更喜欢用ModelForms和这样做,而不需要创建一个涉及的模型的实例。
Now I want to do the opposite: a form to edit/create authors with an inline formset of books to edit/create. How do I do that? I would really prefer a way to do this with ModelForms and without the need to create an instance of one of the Models involved.
例如:
- 作者名称:
[________]
- 书籍:
- 标题:
[________] code>
- 标题:
[________]
- 标题:
- (添加更多)
- Author name:
[________]
- Books:
- Title:
[________]
- Title:
[________]
- Title:
[________]
- (add more)
注意:我没有任何作者的实例需要这个在这一点上。
Note: I need this without any instances of Authors at this point.
我可能错过了一些简单的东西,我在文档上花费了太多时间。欢迎一点帮助!
I'm probably missing out on something simple here and I've spent a bit too much time on the documentation here. A bit of help is welcome!
推荐答案
我将使用
ModelForm
for作者
和ModelFormset
forBook
in the相同的看法。如下所示:I would do this using a
ModelForm
forAuthor
and aModelFormset
forBook
in the same view. Something like this:in forms.py: AuthorForm = modelform_factory(Author) BooksFormset = modelformset_factory(Book, extra=3, fields=('title',)) in views.py: if request.method == POST: author_form = AuthorForm(request.POST) books_formset = BooksFormset(request.POST) if author_form.is_valid() and books_formset.is_valid(): author = author_form.save() new_books = books_formset.save(commit=False) for new_book in new_books: new_book.author = author new_book.save() # not actually needed with these models, but a good habit to include nonetheless books_formset.save_m2m() return redirect('some-success-view') else: author_form = AuthorForm() books_formset = BooksFormset(queryset=Book.objects.none()) # or give a different initial queryset if you want some preselected choice extra_context = {'author_form': author_form, 'books_formset': books_formset} return render(request, 'some_template', extra_context)
内联表单基本上为您实现了作者实例和书籍实例之间的绑定。在内部,内联格式集使用基于您给出的父模型的实例的查询集 - 我认为在
中
中传递
instance = None
/ code>代码路径,如果作者表单在中没有验证,
将会工作,但我从来没有测试过。Inline formsets basically do the binding between the author instance and the books instance for you. Internally, the inline formset uses a queryset based on the instance of the parent model you give it - I think passing in
instance=None
in theget
code path and if the author form doesn't validate in thepost
would work, but I've never tested that.此外,我没有在这里处理添加更多行为 - 管理员从Jquery插件中获取该行为,并且应该很容易复制到您自己的表单中。
Also, I'm not tackling the 'add more' behavior here - the admin gets that behavior from a Jquery plugin, and that should be easy to copy onto your own form.
这篇关于在Django中如何得到内联表单的相反方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Title:
- 标题: