我无法显示新创建的表(blog_sp500_post_noid_sorted)。每当我尝试在管理页面下打开表时,我收到以下错误消息:
OperationalError at /admin/blog/sp500_post_noid_sorted/
no such column: blog_sp500_post_noid_sorted.id
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/blog/sp500_post_noid_sorted/
Django Version: 1.8
Exception Type: OperationalError
Exception Value:
no such column: blog_sp500_post_noid_sorted.id
Exception Location: C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 318
Python Executable: C:\Python27\python.exe
Python Version: 2.7.9
Python Path:
['C:\\Python27\\djangogirls\\mysite',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages']
Server time: Sun, 15 Apr 2018 11:30:20 -0700
通过使用SQLite命令从数据库中的现有表插入列来创建表。 我注意到总是为Django中的模型创建一个'id'列。但是,对于使用SQLite命令创建的表,不存在此类id列。 Django有没有办法忽略id列的缺失,还是我必须手动创建id列?
在通过SQLite命令创建表后添加了以下模型:
class sp500_post_NoID_sorted(models.Model):
Day = models.DateField(blank=False, default=datetime.date.today().strftime('%Y-%m-%d'))
Symbol = models.CharField(max_length=20, default='N/A')
LastPrice = models.CharField(max_length=30, default='N/A')
FiftyTwoWkChg = models.CharField(max_length=30, default='N/A')
FiftyTwoWkHi = models.CharField(max_length=30, default='N/A')
FiftyTwoWkLo = models.CharField(max_length=30, default='N/A')
DivYild = models.CharField(max_length=30, default='N/A')
TrailPE = models.CharField(max_length=30, default='N/A')
ForwardPE = models.CharField(max_length=30, default='N/A')
PEG_Ratio = models.CharField(max_length=30, default='N/A')
PpS = models.CharField(max_length=30, default='N/A')
PpB = models.CharField(max_length=30, default='N/A')
Market_Cap = models.CharField(max_length=30, default='N/A')
Free_Cash_Flow = models.CharField(max_length=30, default='N/A')
Market_per_CashFlow = models.CharField(max_length=30, default='N/A')
Enterprise_per_EBITDA = models.CharField(max_length=30, default='N/A')
Name = models.CharField(max_length=50, default='N/A')
答案 0 :(得分:0)
如果你的模型是在Django中定义的,你应该能够在适当的列上设置主键。
请参阅documentation并向下滚动至primary_key。将该选项添加到作为主键的模型字段中,然后makemigrations并进行迁移。