Pythonanywhere Django MySQL dilemma

Pythonanywhere Django MySQL dilemma
0

You can see I have rows (cw is the Django app name):
mysql> select * from cw_operator;
±-----±----------±---------+
| id | name | slug |
±-----±----------±---------+
| 6827 | CHAIN OIL | chainoil |
| 4598 | BLAIR H H | blairhh |
±-----±----------±---------+
2 rows in set (0.00 sec)

But the Django shell insists on prepending “default.” to my Django model name:
In [3]: from cw.models import *
In [4]: Operator.objects.all()
ProgrammingError: (1146, “Table ‘mesas676$default.cw_operator’ doesn’t exist”)

OK I got it. Here’s what I did:


  1. If the Django shells insist on prepending ‘magula4’ to the model name, why not prepend magula4 to the table name when I create the table? Click on mesas676$magula4 database on PA Databases tab:

mysql> CREATE TABLE mesas676$magula4.cw_operator (id INT, name CHAR(255), slug CHAR(255));
mysql> INSERT INTO mesas676$magula4.cw_operator (id,name,slug) VALUES(4255,‘ARMER M B’, ‘armermb’);
mysql> SELECT * FROM mesas676$magula4.cw_operator;
±-----±----------------±--------------+
| id | name | slug |
±-----±----------------±--------------+
| 4255 | ARMER M B | armermb |
±-----±----------------±--------------+

  1. ALTER TABLE as needed until you have all the fields that are in the Django model e.g.:
    mysql> ALTER TABLE mesas676$magula4.cw_operator ADD COLUMN wells BOOLEAN;

  2. change Django DATABASES settings to magula4:
    DATABASES = {
    ‘default’: {
    ‘ENGINE’: ‘django.db.backends.mysql’,
    ‘NAME’: ‘mesas676$magula4’,
    ‘USER’: ‘mesas676’,
    ‘PASSWORD’ : ‘h&secret9*’,
    ‘HOST’: ‘mesas676.mysql.pythonanywhere-services.com’,
    ‘PORT’: ‘3306’,
    }
    }

  3. Django shell:
    $ ./manage.py shell
    In [4]: from cw.models import *
    In [5]: o=Operator.objects.get(id=4255)
    In [6]: o.name
    Out [14]: ‘ARMER M B’
    hurrah!