I'm getting a problem with Django Chartit
from my views.py file because he doesn't find my table. However this table exists in my MySQL Database
.
I'm using this module in order to display some graphics in my Django website and it's the first time I'm using it.
I have a table which is named Person
in my Identity
application like this :
class Person(models.Model):
title = models.CharField(max_length=12,choices=TITLE_CHOICES, verbose_name='Civilité')
young_girl_lastname = models.CharField(max_length=30, verbose_name='Nom de jeune fille', blank=True)
lastname = models.CharField(max_length=30, verbose_name='Nom de famille')
firstname = models.CharField(max_length=30, verbose_name='Prénom(s)')
sex = models.CharField(max_length=8, choices=SEX_CHOICES, verbose_name='Sexe')
birthday = models.DateField(verbose_name='Date de naissance')
birthcity = models.CharField(max_length=30, verbose_name='Ville de naissance')
birthcountry = CountryField(blank_label='Sélectionner un pays', verbose_name='Pays de naissance')
.....
In my MySQL Database, I have this :
mysql> show tables ;
+-------------------------------------+
| Tables_in_DatasystemsEC |
+-------------------------------------+
| BirthCertificate_birthcertificate |
| Configurations_theme |
| Identity_monthlyweatherbycity |
| Identity_person |
| Mairie_mairie |
| Recensement_attestation_recensement |
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
| log_userprofile |
+-------------------------------------+
You have Identity_person
as table.
In my view, I defined a new function in order to display some statistics graphics :
def Chartview(request) :
#Step 1: Create a DataPool with the data we want to retrieve.
ds = \
DataPool(
series=
[{'options': {
'source': Person.objects.raw(
"SELECT birthcountry, COUNT(birthcountry) as nombre FROM Person GROUP BY birthcountry")
},
'terms': [
'birthcountry',
'nombre']}
])
#Step 2: Create the Chart object
cht = Chart(
datasource = ds,
series_options =
[{'options':{
'type': 'column',
'stacking': False},
'terms':{
'birthcountry': [
'nombre']
}}],
chart_options =
{'title': {
'text': 'Nombre de naissances par pays'},
'xAxis': {
'title': {
'text': 'Pays de naissance'}}})
return render(request, 'statistics.html',{'birthcountrychart': cht})
But I get this error :
(1146, "Table 'DatasystemsEC.Person' doesn't exist")
I don't understand by what I have to replace : Person.objects.raw
. I tried Identity_person.objects.raw
but it doesn't work.
Thank you by advance
EDIT :
I found another way to make my query with Django Aggregation thanks to @e4c5 and it works pretty well :
Person.objects.values('birthcountry').annotate(nombre = Count('birthcountry'))}
I think you should write your SQL as:
'source': Person.objects.raw(
"SELECT birthcountry, COUNT(birthcountry) as nombre FROM Identity_person GROUP BY birthcountry")
},
Mysql table names are case sensitive on most installations. The standard django table nomenclature is appname_tablename
in all lower case so your should ideally have the following in your model:
class Meta:
db_table = 'Identity_person'
Then your raw query also should have the proper table name
SELECT birthcountry, COUNT(birthcountry) as nombre FROM Identity_person GROUP BY birthcountry
I would however argue that you should use a django aggregation instead of a raw query here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.