Well, to start you can use a simple search with django in your client model:
Clientes.objects.filter(medio='whatever').count()
The filter
function filters all the objects in the model according to the search you need, the count
function counts all the objects found in that search.
For more information you can use the documentation: Filter in Django
Now, you need to use chartkick
what you want is to create a dictionary with the names of each medium and a number of clients of each medium, for this you can make a list or an arrangement with all the names of the media , or failing that, do a search to find the existing media in the client, and then do an iteration that creates an arrangement with the name of said medium and as a value the search that I put before you, can be in the following manner:
var data = []
for medio in medios:
data.append(medio, Clientes.objects.filter(medio=medio).count()))
Assuming that data
will be the list of data that the library will use, as it says in its documentation: [[' Chrome ', 52.9], [' Firefox ', 27.7], [' Opera ', 1.6]]
We create a media list that will have the name of the medium as objects, and in an iteration we will add to the data list the average value obtained in media and the number of objects that the media list has.
Broadly speaking, it should be a solution of that style. The last step would be to call the list generated from Django, but for this you can use this list in a class-based view as an additional context.
For this we can see the Sample List View in the documentation:
def get_context_data(self, **kwargs):
context = super(ArticleListView, self).get_context_data(**kwargs)
context['now'] = timezone.now()
return context
In this function we can add the additional content that we want the template to show on the screen:
def get_context_data(self, **kwargs):
context = super(ArticleListView, self).get_context_data(**kwargs)
var data = []
for medio in medios:
data.append(medio, Clientes.objects.filter(medio=medio).count()))
context['data'] = data
return context
With this we will pass the variable data as the context data in the list view, or if you want, in a detailed view, and that would be enough.