我刚开始使用 django 创建自己的应用程序教程(创建投票)我有点偏离,因为我想使用我自己已经存在的数据库模型创建一个应用程序。
在教程中它说
- 表名是通过组合应用程序的名称(polls)和模型的小写名称——poll 和choice 自动生成的。(您可以覆盖此行为。)
- 主键 (ID) 会自动添加。(您也可以覆盖它。)
- 按照惯例,Django 将“_id”附加到外键字段名称。是的,您也可以覆盖它。
但是我看不到它在哪里提到如何覆盖这种行为?我已经这样定义了我的模型
from django.db import models
# Create your models here.
class Channels(models.Model):
channelid = models.IntegerField()
channelid.primary_key = True
channelname = models.CharField(max_length=50)
现在当我进入外壳时,这就是我得到的
>>> from tvlistings.models import *
>>> Channels.objects.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python26\lib\site-packages\django\db\models\query.py", line 67, in __
repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "C:\Python26\lib\site-packages\django\db\models\query.py", line 82, in __
len__
self._result_cache.extend(list(self._iter))
File "C:\Python26\lib\site-packages\django\db\models\query.py", line 271, in i
terator
for row in compiler.results_iter():
File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 67
7, in results_iter
for rows in self.execute_sql(MULTI):
File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 73
2, in execute_sql
cursor.execute(sql, params)
File "C:\Python26\lib\site-packages\django\db\backends\util.py", line 15, in e
xecute
return self.cursor.execute(sql, params)
File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 86
, in execute
return self.cursor.execute(query, args)
File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "C:\Python26\lib\site-packages\MySQLdb\connections.py", line 35, in defau
lterrorhandler
raise errorclass, errorvalue
DatabaseError: (1146, "Table 'tvlistings.tvlistings_channels' doesn't exist")
显然它找不到表 tvlistings_channels 因为它实际上被称为频道。那么如何更改默认值呢?