python - 返回 DateTimeField 会导致 OperationError
问题描述
from django.db import models
# Create your models here.
class TheDate(models.Model):
"""A topic the user is learning about"""
theDate = models.DateTimeField()
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""returns a string representation of the model"""
return self.theDate
即使尝试访问值存储或尝试保存新字段也会导致以下回溯:
OperationalError at /admin/meal_plans/thedate/
no such table: meal_plans_thedate
Request Method: GET
Request URL: http://localhost:8000/admin/meal_plans/thedate/
Django Version: 2.2
Exception Type: OperationalError
Exception Value:
no such table: meal_plans_thedate
Exception Location: C:\Users\$$$\Desktop\meal_planner\ll_env\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 383
Python Executable: C:\Users\$$$\Desktop\meal_planner\ll_env\Scripts\python.exe
Python Version: 3.8.3
Python Path:
['C:\\Users\\$$$\\Desktop\\meal_planner',
'C:\\Program '
'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.8_3.8.1008.0_x64__qbz5n2kfra8p0\\python38.zip',
'C:\\Program '
'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.8_3.8.1008.0_x64__qbz5n2kfra8p0\\DLLs',
'C:\\Program '
'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.8_3.8.1008.0_x64__qbz5n2kfra8p0\\lib',
'C:\\Users\\$$$\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0',
'C:\\Users\\$$$\\Desktop\\meal_planner\\ll_env',
'C:\\Users\\$$$\\Desktop\\meal_planner\\ll_env\\lib\\site-packages']
Server time: Sun, 28 Jun 2020 13:18:06 +0000
解决方案
该错误表示您没有正确迁移模型。您需要运行manage.py makemigrations
和manage.py migrate
迁移数据库,以便它在数据库端创建表。
此外__str__
应该返回一个str
ing 对象,例如,您可以str(…)
调用self.theDate
:
from django.db import models
# Create your models here.
class TheDate(models.Model):
"""A topic the user is learning about"""
theDate = models.DateTimeField()
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""returns a string representation of the model"""
return str(self.theDate)
推荐阅读
- python - 来自非负数据集的欢乐图中的负值
- php - PHP - 获取当前 UTC 日期时间的正确方法
- r - 用预先选择的颜色分别为 ggplot 中的每个数据点在方框和晶须图中着色
- javascript - XMLHttpRequest 服务器响应状态为 405(不允许方法)
- javascript - react-table 'pivotBy' :如何在 pivotBy 中使用的列上进行过滤
- gradle - 模块不在 Gradle 视图中
- git - git - 推送时在仓库中保留特定文件
- android - 如何从 Android App Incoming Intent 获取 URI
- c - 在哪里可以找到 Apache 用于处理 .htaccess 的源代码?
- python-3.x - 从单词列表中查找给定句子的字谜