制作一个简单的Django模型并在一个页面上显示数据。
在弄乱了我的模型之后,我注解掉了坏代码,删除了除__init__.py
文件之外的所有迁移文件,并删除了db.sqlite3
(如下所述:https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html)。
然而,我开始得到这个错误no such table: django_session
在我的管理页面。
我还有17 unapplied migration(s)
我是怎么搞砸模型的?我输入DecimalType时没有默认值,CLI给了我三个选项:他们赋值,我赋值,或者让它空着。我选择了第一个选项,自从我掉进兔子洞🐇后
有人建议我的db.sqlite3
的路径是错误的,所以下面是我的文件树和设置代码:
型号
from django.db import models
from django.conf import settings
from django.utils import timezone
from multiselectfield import MultiSelectField
from decimal import Decimal
class BuildingProduct(models.Model):
# CHOICES
SCHOOL_CHOICES = [
('MBO', 'MBO'),
('VMBO', 'VMBO'),
('HBO', 'HBO'),
('OPLEIDINGSBEDRIJF', 'Opleidingsbedrijf'),
]
CATEGORY_CHOICES = [
('MATERIALS', 'Materials'),
('COURSE', 'Course'),
]
# DATABASE FIELDS
name = models.CharField(max_length=200)
description = models.TextField()
categories = MultiSelectField(choices=CATEGORY_CHOICES, blank=True)
schooltype = models.CharField(
max_length=20,
choices=SCHOOL_CHOICES,
default='MBO',
)
# price = models.DecimalField(
# max_digits=10, decimal_places=2, default=Decimal('0.0000')
# )
expiration_date = models.DateTimeField(blank=True, null=True)
published_date = models.DateTimeField(default=timezone.now)
# SAVE METHOD
def create(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.name
命令行界面
➜ djangowim source myvenv/bin/activate
(myvenv) ➜ djangowim python manage.py makemigrations building_product
No changes detected in app 'building_product'
(myvenv) ➜ djangowim python manage.py migrate building_product
Operations to perform:
Apply all migrations: building_product
Running migrations:
No migrations to apply.
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
(myvenv) ➜ djangowim python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
December 13, 2019 - 14:14:08
Django version 2.2.8, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[13/Dec/2019 14:14:24] "GET / HTTP/1.1" 200 468
[13/Dec/2019 14:14:24] "GET /static/css/building_product.css HTTP/1.1" 304 0
Internal Server Error: /admin/
Traceback (most recent call last):
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/contrib/sessions/backends/base.py", line 189, in _get_session
return self._session_cache
AttributeError: 'SessionStore' object has no attribute '_session_cache'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: django_session
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 241, in wrapper
return self.admin_view(view, cacheable)(*args, **kwargs)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 212, in inner
if not self.has_permission(request):
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 186, in has_permission
return request.user.is_active and request.user.is_staff
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/utils/functional.py", line 256, in inner
self._setup()
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/utils/functional.py", line 392, in _setup
self._wrapped = self._setupfunc()
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/contrib/auth/middleware.py", line 24, in <lambda>
request.user = SimpleLazyObject(lambda: get_user(request))
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/contrib/auth/middleware.py", line 12, in get_user
request._cached_user = auth.get_user(request)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/contrib/auth/__init__.py", line 182, in get_user
user_id = _get_user_session_key(request)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/contrib/auth/__init__.py", line 59, in _get_user_session_key
return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY])
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/contrib/sessions/backends/base.py", line 54, in __getitem__
return self._session[key]
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/contrib/sessions/backends/base.py", line 194, in _get_session
self._session_cache = self.load()
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/contrib/sessions/backends/db.py", line 43, in load
s = self._get_session_from_db()
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/contrib/sessions/backends/db.py", line 34, in _get_session_from_db
expire_date__gt=timezone.now()
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/db/models/query.py", line 402, in get
num = len(clone)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/db/models/query.py", line 256, in __len__
self._fetch_all()
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/db/models/query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/db/models/query.py", line 55, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1133, in execute_sql
cursor.execute(sql, params)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Users/Tara/ProgrammingProjects/djangowim/myvenv/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: django_session
[13/Dec/2019 14:14:26] "GET /admin/ HTTP/1.1" 500 194446
http://127.0.0.1:8000/admin/
上的管理页面错误
2条答案
按热度按时间mwngjboj1#
魔法:
也可以刷新旧数据库,然后进行迁移
nbysray52#
1.删除每个应用程序上的数据库、文件缓存和文件迁移
1.冲洗
1.逐个为每个应用程序执行迁移
1.迁移