Django的表单集相当于angular.js [英] Django formset equivalent in angular.js

查看:238
本文介绍了Django的表单集相当于angular.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的Django具有的formsets 时,其中多个形式可被用一个大的形式。所以我们说可以在例如添加图书馆表单集多张书(提供作者和标题)使用相同的书籍形式的重复。

Django has formsets, where multiple forms can be used in one big form. So let's say one can add in a e.g. library formset mulitple books (providing the author and title) using repetitions of the same book form.

如何实现与Angular.js和Django的休息框架相同的功能?我是新来Angular.js和休息的Django框架和需要一些指导如何能在一个大的形式给定的模型来动态地添加更多的形式(比如一本书)(例如:我的图书馆),并将其保存在Django后端

How to achieve the same functionality with Angular.js and Django Rest Framework? I'm new to Angular.js and Django Rest Framework and need some guidance how to be able to dynamically add more forms(e.g. for a book) for a given model in one big form (e.g. my library) and save them in Django Backend.

推荐答案

您可以在2个步骤实现这一点:

You can achieve this in 2 steps:

创建一个<形式为GT; 您的网页,因为你需要将构建由用户输入的数据上。里面那个<形式> 元素,你需要在 ngForm 多形式验证的行为正确使用(这里是如何一个很好的解释 ngForm 作品)。一个假设code段将如下所示:

Create a <form> on your page that will structure the data entered by the user as you need. Inside that <form> element, you'll need to use the ngForm for multiple forms' validation to behave correctly (here is a nice explanation of how ngForm works). A hypothetical code snippet would look like:

<form name="libraryForm">
  <div ng-repeat="book in vm.newBooksToAdd">

    <!-- ngForm directive allows to create forms within the parent form -->
    <ng-form name="bookForm">
      <div>
        <label>Book title</label>
        <input ng-model="book.title" type="text" name="title" required>
      </div>
      <div>
        <label>Author</label>
        <input ng-model="book.author" type="text" name="author" required>
      </div>
    </ng-form>
  </div>
</form>

在你的控制器,可以初始化的书籍列表添加为 vm.newBook​​sToAdd = []; ,只要你想,以一种新的形式添加到您的列表新书形式,就 vm.newBook​​sToAdd.push({})一个空的对象。因此,你将发送给后端要重新创建presenting书籍对象的数组。

In your controller, you can initialize the list of books to add as vm.newBooksToAdd = []; and whenever you want to add a new form to your list of forms for new books, just vm.newBooksToAdd.push({}) an empty object. Thus, you will send to the backend an array of objects representing books you want to create.

现在在这里你需要覆盖您的视图,允许一次创建许多情况下,因为默认情况下,预计单个对象的 .create()方法。您的看法可能是这样的:

Now here you'll need to overwrite the .create() method of your view to allow creating many instances at once, because by default it expects a single object. Your view might look like this:

class LibraryViewSet(views.ModelViewSet):
    ...

    def create(self, request):
        serializer = self.get_serializer(data=request.data, many=True)  # notice the `many` keywork argument here
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        return Response(serializer.data, status=status.HTTP_201_CREATED)

注意:如果您想同时允许单个实例创建的的创作散装的,你需要调整你的 .create ()方法来检查的 request.data 的数据类型。

Note: If you would like to allow both a single instance creation and creation in bulk, you'll need to adjust your .create() method to check for the data type of request.data.

注意2:有一个 Django的休息框架散装 是实现你想在后台什么,但我没有尝试,所以什么都不能说好还是坏它的库。

Note 2: There is a django-rest-framework-bulk library that achieves what you want on the backend, but I didn't try it, so cannot say anything bad or good about it.

祝你好运!

这篇关于Django的表单集相当于angular.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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