Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改django默认模型设置_Python_Mysql_Sql_Django_Mysql Error 1146 - Fatal编程技术网

Python 更改django默认模型设置

Python 更改django默认模型设置,python,mysql,sql,django,mysql-error-1146,Python,Mysql,Sql,Django,Mysql Error 1146,我只是从django创建您自己的应用程序教程(创建投票)开始,我有点偏离,因为我想使用我自己已经存在的数据库模型创建一个应用程序 在教程中它说 表名将自动删除 通过组合 应用程序(轮询)和小写字母 模型的名称--轮询和选择。 (您可以覆盖此行为。) 添加主键(ID) 自动地(您可以覆盖 这个也一样。) 按照惯例,Django附加了 “_id”到外键字段 名称是的,你可以覆盖这个, 还有 但我看不出它在哪里提到了你可以如何克服这种行为?我已经这样定义了我的模型 from django.db i

我只是从django创建您自己的应用程序教程(创建投票)开始,我有点偏离,因为我想使用我自己已经存在的数据库模型创建一个应用程序

在教程中它说

  • 表名将自动删除 通过组合 应用程序(轮询)和小写字母 模型的名称--轮询和选择。 (您可以覆盖此行为。)
  • 添加主键(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.models导入*
>>>Channels.objects.all()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Python26\lib\site packages\django\db\models\query.py”,第67行,在__
报告__
数据=列表(自身[:报告输出大小+1])
文件“C:\Python26\lib\site packages\django\db\models\query.py”,第82行,在__
莱恩__
self.\u result\u cache.extend(列表(self.\u iter))
文件“C:\Python26\lib\site packages\django\db\models\query.py”,第271行,在i
畸形儿
对于编译器.results\u iter()中的行:
文件“C:\Python26\lib\site packages\django\db\models\sql\compiler.py”,第67行
7、国际热核聚变实验堆试验结果
对于self.execute_sql(多)中的行:
文件“C:\Python26\lib\site packages\django\db\models\sql\compiler.py”,第73行
2、在执行sql中
cursor.execute(sql,params)
文件“C:\Python26\lib\site packages\django\db\backends\util.py”,第15行,在e中
至尊
返回self.cursor.execute(sql,params)
文件“C:\Python26\lib\site packages\django\db\backends\mysql\base.py”,第86行
,执行中
返回self.cursor.execute(查询,参数)
文件“C:\Python26\lib\site packages\MySQLdb\cursors.py”,第166行,在execute中
errorhandler(self、exc、value)
文件“C:\Python26\lib\site packages\MySQLdb\connections.py”,第35行,defau格式
过滤处理程序
提高errorclass,errorvalue
DatabaseError:(1146,“表'tvlistings.tvlistings\u Channel'不存在”)

显然,它找不到表TVU频道,因为它实际上被称为频道。那么如何更改默认值呢?

您可以通过使用

  • 允许您重命名表名
  • 将另一个字段指定为主键将使其使用该字段而不是代理键(不在
    Meta
    类中,仅在模型本身中)

您可以通过使用

  • 允许您重命名表名
  • 将另一个字段指定为主键将使其使用该字段而不是代理键(不在
    Meta
    类中,仅在模型本身中)

在尝试和定制产品之前,您应该尝试并按照自己的方式完成本教程。所有这些内容都包含在实际文档中,但最好在深入研究之前先对这些内容有一个基本的了解


FWIW,这是和上的文件。但实际上,首先要按照编写的那样完成教程。

在尝试和定制之前,你应该尝试按照自己的方式完成整个教程。所有这些内容都包含在实际文档中,但最好在深入研究之前先对这些内容有一个基本的了解

FWIW,这是和上的文件。但实际上,首先要按照编写的那样完成教程