如何从对象的外键中获取对象的值?

Posted

技术标签:

【中文标题】如何从对象的外键中获取对象的值?【英文标题】:How to get a value of a object from its foreign key? 【发布时间】:2016-03-29 00:52:22 【问题描述】:

我正在使用 Backand 来存储我的数据。我有一个对象,事件,它引用另一个对象,位置。

  
    "name": "events",
    "fields": 
      "eventCommentsId": 
        "collection": "comments",
        "via": "eventId"
      ,
      "tags": 
        "collection": "events_tags",
        "via": "event"
      ,
      "users": 
        "collection": "users_events",
        "via": "event"
      ,
      "name": 
        "type": "string"
      ,
      "date": 
        "type": "datetime"
      ,
      "time": 
        "type": "datetime"
      ,
      "info": 
        "type": "text"
      ,
      "locationId": 
        "object": "locations"
      
    ,
  
    "name": "locations",
    "fields": 
      "events": 
        "collection": "events",
        "via": "locationId"
      ,
      "name": 
        "type": "text"
      ,
      "geo": 
        "type": "point"
      
    
  

当我尝试显示事件的位置时,我只能获取 locationID 的值。我想要位置的实际名称,而不是 id。我该怎么做?

<ion-list>
  <ion-item class="item item-thumbnail-left" ng-repeat="event in events" type="item-text-wrap" href="#/event-detail/event.id">
      <h2>event.name</h2>
      <p><i class="ion-location"></i> event.locationId.name</p>
    <ion-option-button class="button-assertive" ng-click="deleteEvent(event.id)">
      Delete
    </ion-option-button>
  </ion-item>
</ion-list>

角码

.service('EventService', function ($http, Backand) 
  var baseUrl = '/1/objects/';
  var objectName = 'events/';

  function getUrl() 
    return Backand.getApiUrl() + baseUrl + objectName;
  

  function getUrlForId(id) 
    return getUrl() + id;
  

  getEvents = function () 
    return $http.get(getUrl());
  ;

  addEvent = function(event) 
    return $http.post(getUrl(), event);
  

  deleteEvent = function (id) 
    return $http.delete(getUrlForId(id));
  ;

  getEvent = function (id) 
    return $http.get(getUrlForId(id));
  ;

  return 
    getEvents: getEvents,
    addEvent: addEvent,
    deleteEvent: deleteEvent,
    getEvent: getEvent
  
)

.controller('FeedCtrl', ['$scope', '$ionicModal', '$ionicSideMenuDelegate', 'EventService',  function($scope, $ionicModal, $ionicSideMenuDelegate, EventService) 

  $scope.events = [];
  $scope.input = ;

  function getAllEvents() 
    EventService.getEvents()
    .then(function (result) 
      $scope.events = result.data.data;
    );
  

  $scope.addEvent = function() 
    EventService.addEvent($scope.input)
    .then(function(result) 
      $scope.input = ;
      getAllEvents();
    );
  

  $scope.deleteEvent = function(id) 
    EventService.deleteEvent(id)
    .then(function (result) 
      getAllEvents();
    );
  

  getAllEvents();

])

【问题讨论】:

【参考方案1】:

有两种选择。您可以使用每个对象的 __metadata 中的描述性值,如下所示: 请求:https://api.backand.com/1/objects/events?pageSize=20&pageNumber=1

回复:


  "totalRows": 2,
  "data": [
    
      "__metadata": 
        "id": "1",
        "fields": 
          "id": 
            "type": "int",
            "unique": true
          ,
          "name": 
            "type": "string"
          ,
          "date": 
            "type": "datetime"
          ,
          "time": 
            "type": "datetime"
          ,
          "info": 
            "type": "text"
          ,
          "locationId": 
            "object": "locations"
          
        ,
        "descriptives": 
          "locationId": 
            "label": "Madison Square Garden",
            "value": "1"
          
        ,
        "dates": 
          "date": "",
          "time": ""
        
      ,
      "id": 1,
      "name": "knicks vs warriors",
      "date": null,
      "time": null,
      "info": "",
      "locationId": "1"
    ,
    
      "__metadata": 
        "id": "2",
        "fields": 
          "id": 
            "type": "int",
            "unique": true
          ,
          "name": 
            "type": "string"
          ,
          "date": 
            "type": "datetime"
          ,
          "time": 
            "type": "datetime"
          ,
          "info": 
            "type": "text"
          ,
          "locationId": 
            "object": "locations"
          
        ,
        "descriptives": 
          "locationId": 
            "label": "Madison Square Garden",
            "value": "1"
          
        ,
        "dates": 
          "date": "",
          "time": ""
        
      ,
      "id": 2,
      "name": "knicks vs cavs",
      "date": null,
      "time": null,
      "info": "",
      "locationId": "1"
    
  ]

或者你可以做一个深度请求并获取相关对象中的值 请求:https://api.backand.com/1/objects/events?pageSize=20&pageNumber=1&deep=true

回复:


  "totalRows": 2,
  "data": [
    
      "__metadata": 
        "id": "1",
        "fields": 
          "id": 
            "type": "int",
            "unique": true
          ,
          "name": 
            "type": "string"
          ,
          "date": 
            "type": "datetime"
          ,
          "time": 
            "type": "datetime"
          ,
          "info": 
            "type": "text"
          ,
          "locationId": 
            "object": "locations"
          
        ,
        "descriptives": 
          "locationId": 
            "label": "Madison Square Garden",
            "value": "1"
          
        ,
        "dates": 
          "date": "",
          "time": ""
        
      ,
      "id": 1,
      "name": "knicks vs warriors",
      "date": null,
      "time": null,
      "info": "",
      "locationId": "1"
    ,
    
      "__metadata": 
        "id": "2",
        "fields": 
          "id": 
            "type": "int",
            "unique": true
          ,
          "name": 
            "type": "string"
          ,
          "date": 
            "type": "datetime"
          ,
          "time": 
            "type": "datetime"
          ,
          "info": 
            "type": "text"
          ,
          "locationId": 
            "object": "locations"
          
        ,
        "descriptives": 
          "locationId": 
            "label": "Madison Square Garden",
            "value": "1"
          
        ,
        "dates": 
          "date": "",
          "time": ""
        
      ,
      "id": 2,
      "name": "knicks vs cavs",
      "date": null,
      "time": null,
      "info": "",
      "locationId": "1"
    
  ],
  "relatedObjects": 
    "locations": 
      "1": 
        "__metadata": 
          "id": "1",
          "fields": 
            "id": 
              "type": "int",
              "unique": true
            ,
            "events": 
              "collection": "events",
              "via": "locationId"
            ,
            "name": 
              "type": "text"
            ,
            "geo": 
              "type": "point"
            
          ,
          "descriptives": ,
          "dates": 
        ,
        "id": 1,
        "events": null,
        "name": "Madison Square Garden",
        "geo": [
          40.7505,
          73.9934
        ]
      
    
  

搜索 Madison Square Garden 作为位置名称以了解 JSON 结构。 您可以在对象设置中设置描述性字段

【讨论】:

我如何获得描述性值? 以及相关的对象值?

以上是关于如何从对象的外键中获取对象的值?的主要内容,如果未能解决你的问题,请参考以下文章

如何像管理面板一样在 Django 的外键中添加新项目?

如何在Laravel中查看外键中的表字段

使用 *ngFor 从对象键中获取对象值

如何将 Django 模型对象转换为字典并且仍然拥有它们的外键? [复制]

Django,在模型外键中按组过滤用户

Django:如何在手动表单字段中显示模型的外键对象作为选择