【问题标题】:How to create CRUD using Django/Tastypie API?如何使用 Django/Tastypie API 创建 CRUD?
【发布时间】:2013-11-02 18:01:17
【问题描述】:

我正在使用 django-tastypie REST API 和 AngularJS 建立一个项目。我可以通过角度从 json 文件中读取内容,但是我找不到一个像样的教程来告诉我如何制作一个简单的 CRUD 应用程序,它不会将所有信息保存在对象或其他任何东西中,而是在进行操作通过tastepie api 的数据库。你们中的任何人都可以给我看一个这样的教程,或者只是给我看一些示例代码吗?

谢谢。

【问题讨论】:

    标签: javascript python django api angularjs


    【解决方案1】:

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

    假设您有 Django 模型 Book 和名为 BookResource 的美味派资源。它的 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 发送请求(最后不带斜线),你会从 sweetpie 得到 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 2012-10-11
      • 1970-01-01
      • 2012-08-12
      • 2014-03-17
      • 2019-07-11
      • 2023-03-22
      相关资源
      最近更新 更多