简体   繁体   English

我可以从AngularJS调用Django管理器方法吗?

[英]Can I call Django Manager Methods from AngularJS?

Is it possible to call Django Manager Methods from AngularJS? 是否可以从AngularJS调用Django管理器方法?

For example like this factory PUT method called in AngularJS: 例如在AngularJS中调用的工厂PUT方法:

var app = angular.module('app', ['ngResource', 'ngRoute']);
// Some APIs expect a PUT request in the format URL/object/ID
// Here we are creating an 'update' method
app.factory('Notes', ['$resource', function($resource) {
return $resource('/notes/:id', null,
    {
        'update': { method:'PUT' }
    });
}]);
// snipped ...
Notes.update({ id:$id }, note);

But for Django's get_or_create for example? 但是以Django的get_or_create为例?

So instead of this in Django: 因此,而不是在Django中:

obj, created = MyObject.objects.get_or_create(**kwargs)

I'd like to do something like: 我想做类似的事情:

app.factory('Notes', ['$resource', function($resource) {
return $resource('/notes/:id', null,
    {
        'get_or_create': // some code here but do not know what??
    });
}]);
// snipped ...
Notes.get_or_update({ id:$id }, note);

I'd like to do this because I have written a good deal of custom Django Managers and would like to use AngularJS for the UI, but don't want to re-write them all if necessary. 我想这样做是因为我已经编写了很多自定义Django管理器,并且希望将AngularJS用于UI,但是不想在必要时全部重写它们。

Thanks 谢谢

The point is that Angular is a client-side framework, and Django is server side. 关键是Angular是客户端框架,而Django是服务器端。 The only way they can communicate is via HTTP, which means that if you want Angular to call a Django manager method, you need to expose that via a URL connected to a view. 他们唯一可以通信的方法是通过HTTP,这意味着,如果您希望Angular调用Django管理器方法,则需要通过连接到视图的URL来公开它。 As the comments say, one way to do that is via Django REST framework, but if that's overkill for your use case you can always write the views yourself. 就像评论中所说的那样,一种方法是通过Django REST框架,但是如果这对于您的用例而言过于刻板,那么您始终可以自己编写视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM