简体   繁体   English

如何在Django中使用用户过滤的数据库中的数据生成气泡图(Chart.js)?

[英]How to generate bubble chart (Chart.js) using data from database FILTERED by user in Django?

I'm new to Django and stuck in one place that didn't seem to be such a problem. 我是Django的新手,只停留在一个似乎不是问题的地方。 I'm trying to do dynamic bubble chart using Python, Django, MySQL and Chart.js. 我正在尝试使用Python,Django,MySQL和Chart.js进行动态气泡图。 I have organizations stored in my database. 我的数据库中存储了组织。 They have some attributes like name, income, amount of recipients, etc.I'm showing it to user and he can filter it (for ex he can display organizations that only have more recipients than specified by user). 它们具有一些属性,例如名称,收入,收件人数量等,我正在向用户显示它,他可以对其进行过滤(例如,他可以显示仅收件人多于用户指定的组织)。 Then I want to create a bubble chart that shows these organizations. 然后,我想创建一个显示这些组织的气泡图。 I managed to do that, but using all database records (static solution, without user interaction). 我设法做到了,但是使用了所有数据库记录(静态解决方案,无需用户交互)。 So, on my bubble chart now I can see everything and I would like to make it dynamic - dependant of user input (showing only organizations that are filtered). 因此,现在在气泡图上,我可以看到所有内容,并且希望使其动态化-取决于用户输入(仅显示已过滤的组织)。 Filtered data are in filter.qs in myuserlist1.html file. 过滤的数据位于myuserlist1.html文件的filter.qs中。 Now I use the code below in my views.py to have all records from database in JSON format under defined URL: 现在,我在views.py中使用以下代码,以JSON格式将数据库中的所有记录都定义为URL:

class ChartData(APIView):

    authentication_classes = []
    permission_classes = []


    def get(self, request, format=None):
        organizations = Opp.objects.all().values()
        data = list(organizations)  
        return Response(data)

    def search(request):
        user_list = User.objects.all()
        user_filter = UserFilter(request.GET, queryset=user_list)
        return render(request, 'user_list1.html', {'filter': user_filter})'''

Url that I use for store results: 我用于存储结果的网址:

url(r'^api/chart/data/$', ChartData.as_view()),

This is part of my chart.html file: 这是我的chart.html文件的一部分:

<script>
    {% block jquery %}
    var endpoint = '/api/chart/data/'
    var endpoint= form.attr('action')

    var dynamicColors = function() {
        var r = Math.floor(Math.random() * 255);
        var g = Math.floor(Math.random() * 255);
        var b = Math.floor(Math.random() * 255);
        return "rgb(" + r + "," + g + "," + b + ")";
    }
    $.ajax({
        method: "GET",
        url: endpoint,
        success: function(data){
            //...... some logic there
        }
    })

</script>  

I don't know how to approach it. 我不知道该怎么办。 I thought to make JSON from filtered data an locate it under URL instead of all data from database but I don't know how to do it. 我本想使过滤后的数据中的JSON定位在URL下,而不是数据库中的所有数据下,但是我不知道该怎么做。 I don't know how to access filtered queryset from View. 我不知道如何从视图访问过滤的查询集。 Any help would be appreciated. 任何帮助,将不胜感激。 Thank you in advance! 先感谢您!

You can make HTTP POST request instead of GET to pass JSON data to a backend. 您可以发出HTTP POST请求而不是GET请求,以将JSON数据传递到后端。 For example: 例如:

$.ajax({
    method: "POST",
    url: endpoint,
    data: {
        "organisation": "organisation_name"
    },
    success: function(data){
        //...... some logic there
    }
})

This would pass data as JSON serialized object to Django view. 这会将data作为JSON序列化对象传递给Django视图。

And then on the Django view you can deserialize the organisation and filter the model: 然后,在Django视图上,您​​可以反序列化组织并过滤模型:

import json

def get(self, request, format=None):
    json_data = json.loads(request.body)
    organisation_name = json_data.get("organisation")
    organizations = Opp.objects.filter(name=organisation_name).values()
    data = list(organizations)  
    return Response(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM