Python 如何从Json对象的外键获取名称值

Python 如何从Json对象的外键获取名称值,python,django,angularjs,Python,Django,Angularjs,我在我的一个项目中使用了Django。我也使用了angularjs。提到这里,我对Django和Angularjs非常陌生。现在这是我的一个视图,我从一个模型中检索数据,即SubscribePlan def subscribed(request): from django.core import serializers subscribedplan = SubscribePlan.objects.filter(user = request.user) subscribin

我在我的一个项目中使用了Django。我也使用了angularjs。提到这里,我对Django和Angularjs非常陌生。现在这是我的一个视图,我从一个模型中检索数据,即SubscribePlan

def subscribed(request):
    from django.core import serializers
    subscribedplan = SubscribePlan.objects.filter(user = request.user)
    subscribing = serializers.serialize('json',subscribedplan)

    return HttpResponse(subscribing)
订阅计划的models.py为

class SubscribePlan(models.Model):
    plan = models.ForeignKey(Plan)
    expiriary_date = models.DateTimeField()
    user = models.ForeignKey(User)
在这里你可以看到,计划是一个外键。现在我已经在我的模板中使用AnuglarJ来获取数据,以显示使用上述给定视图订阅的相关用户的计划名称

{% extends 'userena/base_userena.html' %}

{% load i18n %}
{% load url from future %}

{% block title %}{% blocktrans with profile.user.username as username %}{{ username }}'s profile.{% endblocktrans %}{% endblock %}
{% block content_title %}<h2>{{ profile.user.username }} {% if profile.user.get_full_name %} ({{ profile.user.get_full_name }}){% endif %}</h2>{% endblock %}

{% block content %}


   <div class="panel-body">

     <h4 ng-controller='SubscribedPlanCtrl'>Subscribe Plan: <span style = "color:red">{[{subscribedplan.0.plan}]}</span></h4>

   </div>

{%block extrajs%}
    <script type="text/javascript">
var app = angular.module('shutter',[]);
app.config(function($httpProvider){
//$httpProvider.defaults.headers.post['X-CSRFToken'] = {% csrf_token %};
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
});
app.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('{[{');
$interpolateProvider.endSymbol('}]}');
});   




app.controller('SubscribedPlanCtrl',['$scope','$http',function($scope,$http){

  $http.get('/showphoto/subscribing/').success(function(subdata){

    $scope.subscribedplan = subdata;

   }).error(function(subdata,status){
        $scope.datas = subdata || "Request Fail";
        $scope.status = status;
        console.log(status);


});
}]);
 </script>
{%endblock%}            
{% endblock %}
{%extends'userena/base_userena.html%}
{%load i18n%}
{%从未来加载url%}
{%block title%}{%blocktrans,其中profile.user.username作为username%}{{{username}}的配置文件。{%endblocktrans%}{%endblock%}
{%block content_title%}{{profile.user.username}{%if profile.user.get_full_name%}({{profile.user.get_full_name}}}{%endif%}{%endblock%}
{%block content%}
订阅计划:{[{subscribedplan.0.Plan}]}
{%block extrajs%}
var app=角度模块('快门',[]);
app.config(函数($httpProvider){
//$httpProvider.defaults.headers.post['X-CSRFToken']={%csrf_token%};
$httpProvider.defaults.xsrfCookieName='csrftoken';
$httpProvider.defaults.xsrfHeaderName='X-CSRFToken';
});
app.config(函数($interpolateProvider){
$interpolateProvider.startSymbol('{[{');
$interpolateProvider.endSymbol('}]}');
});   
app.controller('SubscribedPlanCtrl',['$scope','$http',函数($scope,$http){
$http.get('/showphoto/subscripting/')。成功(函数(子数据){
$scope.subscribedplan=子数据;
}).错误(功能(子数据、状态){
$scope.datas=子数据| |“请求失败”;
$scope.status=状态;
控制台日志(状态);
});
}]);
{%endblock%}
{%endblock%}
在模板中,您可以看到我使用了{[{subscribedplan.0.plan}]}来显示计划的名称

def subscribed(request):
    from django.core import serializers
    subscribedplan = SubscribePlan.objects.filter(user = request.user)
    subscribing = serializers.serialize('json',subscribedplan)

    return HttpResponse(subscribing)
但问题是,与其显示计划的名称,不如显示计划的id。请记住计划是外键。现在我如何显示计划的名称

您可以使用Django或自己构造JSON对象。在本例中,后者是可以的,因为它是一个小模型,您不需要反序列化它。示例代码(未测试):

def subscribed(request):
    from django.core import serializers
    subscribedplan = SubscribePlan.objects.filter(user = request.user)
    subscribing = serializers.serialize('json',subscribedplan)

    return HttpResponse(subscribing)

建议使用django angular这样更完整的软件包,以实现更好的集成。

这绝不是关于angular的问题,而是关于django序列化的问题。然后请编辑问题的名称。您可以使用django的自然键或自行序列化对象。您可以解释一下作为答案,因为我已经序列化了对象,所以不理解您建议的序列化对象的方式,所以如果它运行良好,那么我将@arocks接受它