0
votes

Rename the default django table.

I can't change the default table name django_sessions

Could anyone help?

Thanks for listening

init.py

from django.contrib.sessions.models import Session
Session._meta.db_table = "my_session"

File "/webapps/desk/init.py", line 1, in | from django.contrib.sessions.models import Session | File "/usr/local/lib/python3.8/site-packages/django/contrib/sessions/models.py", line 1, in | from django.contrib.sessions.base_session import ( | File "/usr/local/lib/python3.8/site-packages/django/contrib/sessions/base_session.py", line 26, in | class AbstractBaseSession(models.Model): | File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 108, in new | app_config = apps.get_containing_app_config(module) | File "/usr/local/lib/python3.8/site-packages/django/apps/registry.py", line 253, in get_containing_app_config | self.check_apps_ready() |
File "/usr/local/lib/python3.8/site-packages/django/apps/registry.py", line 136, in check_apps_ready | raise AppRegistryNotReady("Apps aren't loaded yet.") | django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

2

2 Answers

0
votes

In your __init__.py file under the app folder place that line. not in the models.

appname/__init__.py

(not in the models.py)

from django.contrib.sessions.models import Session
Session._meta.db_table = "your_session_table_name"
0
votes

The Session model is an internal implementation of Django, and you cannot do that. In any scenario you are thinking about, what you can do is use Model Inheritance.

There are three styles of inheritance that are possible in Django.

  1. Often, you will just want to use the parent class to hold information that you don’t want to have to type out for each child model. This class isn’t going to ever be used in isolation, so Abstract base classes are what you’re after.
  2. If you’re subclassing an existing model (perhaps something from another application entirely) and want each model to have its own database table, Multi-table inheritance is the way to go.
  3. Finally, if you only want to modify the Python-level behavior of a model, without changing the models fields in any way, you can use Proxy models.