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

3
我正在使用django-tastypie REST API和AngularJS设置一个项目。我可以通过angular从json文件中读取内容,但我找不到一个好的教程,能够向我展示如何制作一个简单的CRUD应用程序。这个应用程序不会将所有信息保存在对象或其他地方,而是通过tastypie api操作数据库。你们有没有这样的教程或者能否向我展示一些这方面的示例代码呢?谢谢。
1个回答

0

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

假设您有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的文档提供了更多有关如何非常有效地使用资源的信息。

这里是URL的问题。据我记得,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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接