如何使用Django / Tastypie API创建CRUD?

时间:2013-10-24 06:54:34

标签: javascript python django api angularjs

我正在使用django-tastypie REST API和AngularJS设置项目。我通过angular读取json文件中的内容很好,但我找不到一个像样的教程,它会告诉我如何制作一个简单的CRUD应用程序,它不会保存对象中的所有信息或者其他什么,但是正在操作数据库通过tastypie api。你们中的任何人都可以向我展示这样的教程,或者只是给我看一些示例代码吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

使用$resource - 一个工厂,它创建一个资源对象,允许您与REST服务器端数据源进行交互。

假设你有Django模型 Book ,以及名为 BookResource 的tastypie资源。它的URL是/ api / v1 / book /。如您所知,此URL实际上是一种资源,这意味着您可以使用GET,POST,DELETE等请求操作Book模型中的数据。 你可以"映射"以这种方式将Angular $ resource 添加到此API资源:

someModule.factory('bookResource', ['$resource', function($resource) {
    var apiResourceUrl = "/api/v1/book/:bookId/";
    // id - your model instance's id or pk, that is represented in API resource objects.
    var resource = $resource(apiResourceUrl, {bookId: '@id'}, {
        all: {
            method: 'GET', params: {}, // GET params that will included in request.
            isArray: true, // Returned object for this action is an array (miltiple instances).
        },
        get: {
            method: 'GET',
        },
        // [Define custom save method to use PUT instead of POST.][2]
        save: {
            /* But, the PUT request requires the all fields in object.
            Missing fields may cause errors, or be filled in by default values.
            It's like a Django form save.
            */
            method: 'PUT',
        },
        // [Tastypie use POST for create new instances][3]
        create: {
            method: 'POST',
        },
        delete: {
            method: 'DELETE',
        },
        // Some custom increment action. (/api/v1/books/1/?updateViews)
        updateViews: {
            method: 'GET',
            params: {"updateViews": true},
            isArray: false,
        },
    });
}]);

someModule.controller('bookCtrl', ['$scope', '$routeParams', 'bookResource',
  function ($scope, $routeParams, bookResource) {
    if ("bookId" in $routeParams) {
        // Here is single instance (API's detail request)
        var currentBook = bookResource.get({bookId: $routeParams.bookId}, function () {
            // When request finished and `currentBook` has data.

            // Update scope ($apply is important)
            $scope.$apply(function(){
                $scope.currentBook = currentBook;
            });

            // And you can change it in REST way.
            currentBook.title = "New title";
            currentBook.$save(); // Send PUT request to API that updates the instance
            currentBook.$updateViews();
        });
    }

    // Show all books collection on page.
    var allBooks = bookResource.all(function () {
        $scope.$apply(function(){
            $scope.allBooks = allBooks;
        });
    });

    // Create new
    var newBook = new bookResource({
        title: "AngularJS-Learning",
        price: 0,
    });
    newBook.$save();

}]);

Angular的文档提供了更多关于如何真正令人难以置信地使用资源的信息。

这是网址的问题。我记得,Angular会向/ api / v1 / books / 1发送请求(最后没有斜线),你将从tastypie获得404。我来看看。

[2] http://django-tastypie.readthedocs.org/en/latest/interacting.html#updating-an-existing-resource-put   [3] http://django-tastypie.readthedocs.org/en/latest/interacting.html#creating-a-new-resource-post