【问题标题】:Database table names with DjangoDjango 中的数据库表名
【发布时间】:2013-05-01 13:20:35
【问题描述】:

我已经有一个名为“mydb”的数据库,其中有一个名为“AERODROME”的表。

我的 models.py 看起来像这样:

from django.db import models

class Aerodrome(models.Model):
    Name = models.CharField(max_length=48)
    Latitude = models.DecimalField(decimal_places=4, max_digits=7)
    Longitude = models.DecimalField(decimal_places=4, max_digits=7)

我在views.py 有这个方法:

from django.shortcuts import render
from helloworld.models import Aerodrome

def aerodromes(request):
    return render(request, 'aerodromes.html', {'aerodromes': Aerodrome.objects.all()})

在我的模板文件夹中,我有 aerodromes.html,这也很简单:

<!doctype html>
<html>
    <head>
    </head>
    <body>
        <table>
        {% for aerodrome in aerodromes %}
            <tr>
                <td>{{ aerodrome.Name }}</td>
                <td>{{ aerodrome.Longitude }}</td>
                <td>{{ aerodrome.Latitude }}</td>
            </tr>
            {% endfor %}
        </table>
    </body>
</html>

当我通过浏览器进行测试时,我收到一个错误,因为它看起来像是在使用错误的名称访问表。我的应用程序被称为“helloworld”,因为它是一个测试,而不是访问 mydb.AERODROMES,而是访问 mydb.helloworld_aerodrome(还要注意区分大小写的问题)。

因为我已经填充了数据库,所以我没有运行 syncdb(我知道这不是必需的,但也许这就是问题所在)。

所以,问题是我不知道为什么要在表名中添加“helloworld_”,而且我仍然不确定我到底在哪里修复表名(从那里来区分大小写的问题有“aerodrome”而不是“AERODROMES”)。

这里有什么帮助吗?

【问题讨论】:

    标签: python mysql django


    【解决方案1】:

    来自 django 文档: 强烈建议在通过 db_table 覆盖表名时使用小写表名,尤其是在使用 MySQL 后端时。有关详细信息,请参阅 MySQL 说明。

    https://docs.djangoproject.com/en/1.11/ref/databases/#table-names

    【讨论】:

      【解决方案2】:

      您可以设置db table name in models Meta class。作为

      class Aerodrome(models.Model):
          Name = models.CharField(max_length=48)
          Latitude = models.DecimalField(decimal_places=4, max_digits=7)
          Longitude = models.DecimalField(decimal_places=4, max_digits=7)
      
          class Meta:
              db_table="AERODROMES"
      

      如果您在外部预填充表格。请注意每个记录/字段中的数据是否合适/兼容。

      但是,您必须 syncdb 因为创建了 django 所需的其他表。

      【讨论】:

        【解决方案3】:

        为您的模型明确指定表名应该会有所帮助:

        from django.db import models
        
        class Aerodrome(models.Model):
            Name = models.CharField(max_length=48)
            Latitude = models.DecimalField(decimal_places=4, max_digits=7)
            Longitude = models.DecimalField(decimal_places=4, max_digits=7)
        
            class Meta:
                db_table = 'AERODROMES'
        

        【讨论】:

          【解决方案4】:

          models.py 模型定义中使用 Meta 类 (documentation here):

          class Aerodrome(models.Model):
              Name = models.CharField(max_length=48)
              Latitude = models.DecimalField(decimal_places=4, max_digits=7)
              Longitude = models.DecimalField(decimal_places=4, max_digits=7)
          
              class Meta:
                  db_table = 'AERODROMES'
          

          这将覆盖 SQL 数据库中模型表的默认命名方案。


          还可以添加managed属性来控制python manage.py syncdbpython manage.py flush是否管理表。

          class Aerodrome(models.Model):
              # ...
          
              class Meta:
                  db_table = 'AERODROMES'
                  managed = False
          

          有了这个你可以syncdb而不必担心擦除你的数据。

          【讨论】:

          • 托管是否仅暗示该表是外部填充的,还是暗示无法从应用程序写入该表?
          • 调用 Aerodrome.save() 将按预期工作,前提是数据库表实际上与模型定义相匹配(例如,具有与您的 NameLatitudeLongitude 匹配的正确架构)。来自documentation“如果为False,则不会对该模型执行数据库表的创建或删除操作。...这是managed=False时唯一的区别。模型处理的所有其他方面完全相同正常。”
          猜你喜欢
          • 2014-04-03
          • 2021-02-14
          • 2018-05-24
          • 2011-02-20
          • 2015-12-13
          • 2011-11-09
          • 2023-03-27
          • 2010-09-07
          • 1970-01-01
          相关资源
          最近更新 更多