- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Django REST Framework 对使用大量模型关系的 REST API 进行建模。
使用 AngularJS 和 Restangular 在前端级别解决这些关系的简单而好方法是什么?
楷模
这是我的模型的一部分的样子:
class Country( Model ):
name = CharField( max_length = 256 )
class City( Model ):
name = CharField( max_length = 256 )
postal_code = CharField( max_length = 16 )
country = ForeignKey( "Country" )
class Customer( Model ):
first_name = CharField( max_length = 256 )
last_name = CharField( max_length = 256 )
city = ForeignKey( "City" )
all()
返回的 promise ,但
$object
用于解释这一点更短)
$scope.countries = [];
$scope.cities = [];
$scope.customers = [];
Restangular.all( "countries" ).getList().$object;
Restangular.all( "cities" ).getList().$object;
Restangular.all( "customers" ).getList().$object;
[{"id": 1, "name": "Bocholt", "postal_code": "46397", "country": 1}]
<table>
<tbody>
<tr ng-repeat="customer in customers">
<td>{{ customer.first_name }}</td>
<td>{{ customer.last_name }}</td>
<td>{{ cities[customer.city].name }}</td>
<td>{{ countries[cities[customer.city].country].name }}</td>
</tr>
</tbody>
</table>
最佳答案
我相信最好的方法是创建嵌套的 serailizers。您必须在帖子上切换序列化程序,但在列表中并检索这样的序列化程序会起作用。
class CountrySerializer(serializer.ModelSerializer):
class Meta:
model = Country
class CitySerializer(serializer.ModelSerializer):
country = CountrySerializer()
class Meta:
model = City
class CustomerSerializer(serializer.ModelSerializer):
city = CitySerializer()
class Meta:
model = Customer
class CustomerViewSet(viewsets.ModelViewSet):
model = Customer
serializer_class = CustomerSerializer
queryset = Customer.objects.all()
Restangular.all('costumers').one(1).get().then(function(response) {
$scope.costumer = response;
})
关于django - Restangular、Django REST 和关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25038809/
我正在尝试使用 restangular 作为 rails rest api 的适配器,但我无法使其正常工作。我已经下载了最新版本并将文件放在 vendor/assets/javascripts 文件夹
我正在尝试为我的 Angular 应用程序创建一个编辑 View 。我正在使用 Restangular 作为处理我的 CRUD 操作的服务。 这里的 Plunker 就是一个很好的例子:http://
到目前为止,我使用 Restangular 对我的 API 进行了大约 4 次调用,并且在每个调用中,我都在 then 的第二个参数上检查它们,如下所示: Restangular.all("accou
我需要对 session /身份验证做任何特别的事情吗?通过 retangular post 方法登录后,我的服务器看不到我的 session 。登录成功,但之后在服务器上看不到 session 。如
所以我开始做一个自己的项目,我正在开发我的网站的前端。我从 PHP Laravel 后端开始,并为我的数据库设置了 API 服务。 考虑到混合应用程序,我开始将 angularjs 用于我的前端 We
我正在使用 AngularJS v1.2.16 与 Restangular v1.4.0 ,并想知道是否可以覆盖 ErrorInterceptor。如果是,如何?如果没有,我该如何解决? 我已经配置了
使用restangular处理嵌套资源时的最佳实践是什么? IE。 Restangular.one("accounts", 1).one("projects", 1).get().then(funct
我想寻求帮助,因为我不知道这种情况下的问题是什么。 我有一个简单的 MVC5 应用程序,我想在其中使用 Angular 和矩形。当我加载页面时, Angular Controller 将被执行,但不会
在我的AngularJS项目中,我尝试使用Restangular getList方法,但由于API响应不直接是数组而是包含数组的对象,因此返回错误。 { "body": [ // arra
我正在尝试使用 Restangular 从我的后端检索 JSON 对象。 1) 这很好用,但我不知道如何处理错误,因为没有回调/ promise 函数: meals = restAngular.all
我正处于为现有 REST 服务开发客户端应用程序的早期阶段,我正在尝试决定使用什么进行服务器通信。到目前为止,我喜欢 Restangular 文档,它看起来非常可靠,但我担心它不能与该服务一起使用,因
我正在使用 Decoupled Restangular service用于使用我的 API: // Declare factory module.factory('Users', function(R
我有一个 Restangular 系列,效果很好: $scope.threads = Restangular.all('thread').getList({ limit: 10, offset: 0
我刚刚发现了 Angular“Restangular”库,天哪,它看起来确实很棒!然而,当我用它执行一些简单的 GET 请求时,我遇到了麻烦。我只是在试验并尝试从我的 API 路由之一返回 JSON
我需要在 Restangular 中创建一个相当复杂的查询字符串。 http://vdmta.rd.mycompany.net//people?anr=Smith&attrs=givenName,di
我正在使用带有 AngularJS 应用程序的 Restangular,并从一个简单的 CRUD 应用程序开始。我遇到的问题是了解如何将新项目添加到预先存在的集合中。 在 Controller 中,预
如果我没有弄错 RESTful 服务是为了删除记录,您需要这样做: 删除产品:DELETE/api/product/id ( reference ) 但是在 RESTAngular 中,当我这样做时
我有一个解耦的 Restangular 服务,我想附加一个自定义方法。默认情况下,集合上返回的唯一方法似乎是 getList , one , 和 post .我想做Locations.getLongL
我遇到了 restangular 的问题。此代码: Restangular.one("undefined_ressource").get().then( function(res){
我正在使用一个 API,它要求所有经过身份验证的操作都在请求中包含一个身份验证 token ,但是,我在登录之前没有身份验证 token 。 我只在 app.config 中看到了在 Retangul
我是一名优秀的程序员,十分优秀!