更改 Django 默认模型设置

发布于 2024-10-11 10:22:26 字数 2099 浏览 7 评论 0原文

我刚刚开始使用 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)

现在,当我进入 shell 时,这就是我得到的结果

>>> 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,因为它实际上称为频道。那么如何更改默认值呢?

I'm just starting with the django creating your own app tutorial (creating a Poll) I'm deviating slightly as I'm wanting to create an app using my own database model that already exists.

And in the tutorial it says

  • Table names are automatically
    generated by combining the name of
    the app (polls) and the lowercase
    name of the model -- poll and choice.
    (You can override this behavior.)
  • Primary keys (IDs) are added
    automatically. (You can override
    this, too.)
  • By convention, Django appends
    "_id" to the foreign key field
    name. Yes, you can override this,
    as well.

But I can't see where it mentions how you can override this behaviour? I've defined my model as so

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)

Now when I go in to the shell this is what I get

>>> 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")

Obviously it can't find the table tvlistings_channels as it's actually called channels. So how do I change the default?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2024-10-18 10:22:26

您可以通过使用 内部 Meta 来覆盖 Django 中的模型行为

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

You can override Model behavior in Django largely through the use of an inner Meta class

  • db_table allows you to rename the table name
  • specifying another field as the primary key will have it use that rather than a surrogate key (not in the Meta class, just in the model itself)
柠檬心 2024-10-18 10:22:26

在尝试自定义内容之前,您应该尝试按照本教程的方式进行操作。所有这些内容包含在实际文档中,但最好在深入研究之前先对这些内容有一个基本的了解。

FWIW,这里是关于 定义您自己的文档主键指定表名称。但实际上,请按照首先编写的教程进行操作。

You should try and work your way all through the tutorial before you try and customise things. All these things are covered in the actual documentation, but it's best to have a basic understanding of things first before diving into that.

FWIW, here are the docs on defining your own primary key and specifying a table name. But really, do the tutorial as written first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文