【问题标题】:Getting each values from a value list python从值列表python中获取每个值
【发布时间】:2016-02-05 18:31:30
【问题描述】:

您好,我有一个自定义的 Django/python 资源/视图。如下

class ResourceView(JSONResponseMixin, View):

def get(self, request, *args, **kwargs):
    status = 'error'
    msg = "Success"
    # Getting the x value from url/queryset
    x= self.request.GET.get('x')
    mas = self.request.GET.get('mas')

    queryset_df = Forecast.objects.filter(Q(x=int(fab)) | Q(x=int("0"+x)))\
        .values_list('mas').distinct()

    queryset_or = Record.objects.filter(Q(pc_ext__x=int(x)) | Q(pc_ext__x=int("0"+x)))\
        .values_list('mas').distinct()

    new_mas_list = list(set(list(queryset_df) + list(queryset_or)))
    new_mas_list.sort()
    return self.render_json_response(dict(objects=new_mas_list))

它返回掩码集的值列表。如何获取 valuelist 的每个值(因为在我的 html 中,我有一个下拉列表来显示每个 valuelist 对象,显示正常;但是当我选择一个时,它作为值列表对象返回我作为 ["5300A"] 而不是 5300A。我是使用AngularJS获取值如下:

//UPDATE MAS BASED ON USER FAB
$scope.update_mas = function(){
    $scope.processing['update_mas'] = { msg: 'loading mas..', class: 'text-warning', show: true };
    console.log('Stage1: Loading Mas..... ');
    $http.get('{% url "masresourceview"  %}', { params: { x: $scope.x} }).success(
        function(data){
            $scope.processing['update_mas'] = { msg: 'mas updated.', class: 'text-success' };
            setTimeout(function () { $scope.processing['update_mas'] = null; $scope.$apply(); }, 1000);
            $scope.maslist = data['objects'];
            $scope.isDisable = true;
            console.log($scope.maslist);

        }).error(function(data, status){
            $scope.processing['update_mas'] = { msg: 'Internal Error', class: 'text-danger' };
            console.log('Stage1: Internal error while loading initial data:'+status );
        });
};

我的资源/列表如下所示:

{
objects: [
[
"AMD14NM"
],
[
"BAFFIN1"
],
[
"BAGERA1"
],
[
"ELLSMR1"
],
[
"GARFLD1"
],
[
"GARFLD2"
],

当我输入$scope.maslist = data['objects'][0]; 时,我可以完美地获得第一个对象的值。我可以为对象的其余值做些什么?我需要改进我的views.py/resources吗?提前致谢。

【问题讨论】:

  • 你应该在 html smth 之后使用 `$scope.maslist = data['objects'](your array) 。 jsfiddle.net/nnpngxc1
  • 这里是:jsfiddle.net/jkog5ph0/1 但我更喜欢在显示之前准备数组。例如 Ramda.flatten。 ramdajs.com/docs/#flatten
  • 对不起,展平前的 R 是什么?
  • ramda.js 它是 Ramda 的别名。 ;)
  • 但是你可以使用任何你想要的东西,lodash、下划线等等。

标签: python angularjs django list django-rest-framework


【解决方案1】:

刚刚用flatten更新了返回部分。

from compiler.ast import flatten

return self.render_json_response(dict(objects=flatten(new_mas_list)))

【讨论】:

    【解决方案2】:

    这是一个示例:jsfiddle.net/jkog5ph0/1 但我更喜欢在显示之前准备数组。例如 Ramda.flatten。 ramdajs.com/docs/#flatten

    【讨论】:

      【解决方案3】:

      虽然之前的答案看起来是正确的,但您有一种更简单的方法可以在服务器端处理这个问题。

      values_list有一个 flat 参数来返回一个平面列表而不是列表列表 (https://docs.djangoproject.com/fr/1.8/ref/models/querysets/#values-list)

      因此你的观点应该是:

      类 ResourceView(JSONResponseMixin, View):

      def get(self, request, *args, **kwargs):
          status = 'error'
          msg = "Success"
          # Getting the x value from url/queryset
          fab = self.request.GET.get('x')
          mas = self.request.GET.get('mas')
      
          # DRCForecast Masksets
          queryset_df = Forecast.objects.filter(Q(x=int(fab)) | Q(x=int("0"+x)))\
              .values_list('mas', flat=True).distinct()
          # OrcRecord Masksets
          queryset_or = Record.objects.filter(Q(pc_ext__x=int(x)) | Q(pc_ext__x=int("0"+x)))\
              .values_list('mas', flat=True).distinct()
      
          new_mas_list = list(set(list(queryset_df) + list(queryset_or)))
          new_mas_list.sort()
          return self.render_json_response(dict(objects=new_mas_list))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-10
        • 1970-01-01
        • 2018-03-02
        • 1970-01-01
        相关资源
        最近更新 更多