Can manage a DB after imported it using inspectdb

Hello,

I am not able to manage a database that I created earlier using Django. I’m using a SQLite3 database that I created using the Sqlitestudio application.

I configured my stettings.py as follows:

DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.sqlite3’,
‘NAME’: os.path.join (BASE_DIR, ‘rrpm.db’),
}
}

I imported the DB using the “inspectdb” command as follows:

$ python manage.py inspectdb> gcee.models.py

Models.py was created correctly. However I cannot manipulate the DB using the django API, as it presents the following error:

from gcee.models import *
cliente = Clientes.objects.get(pk=1)
Traceback (most recent call last):
File “”, line 1, in
File “/media/ruben.bisneto@ricoli.local/DADOS/RR ENERGIA/SoftwareDB_RR/RR-PowerManager/Test/venv/lib/python3.6/site-packages/django/db/models/manager.py”, line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File “/media/ruben.bisneto@ricoli.local/DADOS/RR ENERGIA/SoftwareDB_RR/RR-PowerManager/Test/venv/lib/python3.6/site-packages/django/db/models/query.py”, line 411, in get
num = len(clone)
File “/media/ruben.bisneto@ricoli.local/DADOS/RR ENERGIA/SoftwareDB_RR/RR-PowerManager/Test/venv/lib/python3.6/site-packages/django/db/models/query.py”, line 258, in len
self._fetch_all()
File “/media/ruben.bisneto@ricoli.local/DADOS/RR ENERGIA/SoftwareDB_RR/RR-PowerManager/Test/venv/lib/python3.6/site-packages/django/db/models/query.py”, line 1261, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File “/media/ruben.bisneto@ricoli.local/DADOS/RR ENERGIA/SoftwareDB_RR/RR-PowerManager/Test/venv/lib/python3.6/site-packages/django/db/models/query.py”, line 57, in iter
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File “/media/ruben.bisneto@ricoli.local/DADOS/RR ENERGIA/SoftwareDB_RR/RR-PowerManager/Test/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py”, line 1151, in execute_sql
cursor.execute(sql, params)
File “/media/ruben.bisneto@ricoli.local/DADOS/RR ENERGIA/SoftwareDB_RR/RR-PowerManager/Test/venv/lib/python3.6/site-packages/django/db/backends/utils.py”, line 100, in execute
return super().execute(sql, params)
File “/media/ruben.bisneto@ricoli.local/DADOS/RR ENERGIA/SoftwareDB_RR/RR-PowerManager/Test/venv/lib/python3.6/site-packages/django/db/backends/utils.py”, line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File “/media/ruben.bisneto@ricoli.local/DADOS/RR ENERGIA/SoftwareDB_RR/RR-PowerManager/Test/venv/lib/python3.6/site-packages/django/db/backends/utils.py”, line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File “/media/ruben.bisneto@ricoli.local/DADOS/RR ENERGIA/SoftwareDB_RR/RR-PowerManager/Test/venv/lib/python3.6/site-packages/django/db/backends/utils.py”, line 86, in _execute
return self.cursor.execute(sql, params)
File “/media/ruben.bisneto@ricoli.local/DADOS/RR ENERGIA/SoftwareDB_RR/RR-PowerManager/Test/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py”, line 396, in execute
return Database.Cursor.execute(self, query, params)
File “/usr/lib/python3.6/sqlite3/dbapi2.py”, line 64, in convert_date
return datetime.date(*map(int, val.split(b"-")))
ValueError: invalid literal for int() with base 10: b’01/02/2018’

What is going on? What did I do wrong?

Note: This error happened when I was running the python shell using the command:

$ python manage.py shell

It looks like inspectdb has mapped the column containing “01/02/2018” to an IntegerField . Try changing the fields on the generated model. You can also comment fields out.

Thank you very much!

The problem was that I filled all dates filed using the format “dd/mm/yyyy” and Django was trying to manipulate it using “yyyy-mm-dd”.

I’ve just manualy made those changes in the DB. But, I was wondering if there is a way to tell python to use the “dd/mm/yyyy” format. Is that possible?

I think your underlying column is a string (varchar) rather than a date. You should change your database to use a date in the database field. Otherwise, yes there’s a way to tell Django to use “dd/mm/yyyy”, but I believe you’d need to create a custom database field which is a bit of work.

1 Like