Django formset等价于angular.js

[英]Django formset equivalent in angular.js


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.

Django有表单集,其中多个表单可以在一个大表单中使用。因此,我们假设一个人可以添加一个,例如,使用相同的图书形式的重复,提供作者和标题的库形式的mulitple图书(提供作者和标题)。

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.

如何用角度实现同样的功能。js和Django Rest框架?我的新角度。js和Django Rest框架需要一些指导来动态添加更多的表单(例如对于一本书)给定的模型(例如我的库)并将它们保存到Django后端。

1 个解决方案

#1


2  

You can achieve this in 2 steps:

你可以通过以下两个步骤来实现:

On Frontend

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:

在您的页面上创建一个

,它将按照您需要的方式构造用户输入的数据。在这个元素中,您需要使用ngForm对多个表单的验证进行正确的操作(这里有一个关于ngForm如何工作的很好的解释)。假设的代码片段如下:

<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>

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.

在控制器中,可以初始化要作为vm添加的图书列表。newBooksToAdd =[];每当您想要在新图书的表单列表中添加新表单时,只需使用vm.newBooksToAdd.push({})一个空对象。因此,您将向后端发送表示要创建的图书的对象数组。

On Backend

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:

现在,您需要重写视图的.create()方法,以便同时创建多个实例,因为默认情况下,它需要一个对象。您的视图可能是这样的:

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)

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.

注意:如果您希望同时允许单个实例的批量创建和创建,那么需要调整.create()方法来检查request.data的数据类型。

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.

注意2:有一个django-res -frame -bulk库可以在后端实现您想要的内容,但是我没有尝试过,所以不能说它有什么不好或好的地方。

Good luck!

好运!

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2016/03/25/d1ee277fbaa0cae42868ed2ed8366299.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告