Python 从值列表中获取每个值
嗨,我有一个自定义的Django/python资源/视图。如下Python 从值列表中获取每个值,python,angularjs,django,list,django-rest-framework,Python,Angularjs,Django,List,Django Rest Framework,嗨,我有一个自定义的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 = sel
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];我可以完美地得到第一个对象的值。我能为对象的其余值做些什么?我是否需要改进我的视图。py/resources?提前谢谢 刚刚用flatte更新了返回部分 从compiler.ast导入展平
返回self.render_json_responsedictobjects=flattnew_mas_list这里有一个示例:jsfiddle.net/jkog5ph0/1,但我更喜欢在显示数组之前准备数组。比如说拉姆达·弗兰特。ramdajs.com/docs/flatte虽然前面的答案看起来正确,但在服务器端处理这个问题的方法更简单 values\u list有一个平面参数,用于返回平面列表而不是列表列表 因此,你的观点应该是: 类ResourceViewJSONResponseMixin,视图:
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))
您应该在html smth中使用`$scope.maslist=data['objects']数组。这里是:但我更喜欢在显示之前准备数组。比如说拉姆达·弗兰特。对不起,变平前的R是什么?ramda.js是ramda的别名;但是你可以使用任何你想要的,lodash,下划线和其他。