Django的数据库表名称 [英] Database table names with Django

查看:73
本文介绍了Django的数据库表名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一个名为"mydb"的数据库,其中有一个名为"AERODROME"的表.

I already have a database named "mydb", where I have a table called "AERODROME".

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

My models.py looks like this:

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中有此方法:

And I have this method at 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,这也很简单:

At my templates folder, I have aerodromes.html, which is quite simple too:

<!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(也请注意区分大小写的问题).

When I test through my browser, I get an error, because it looks like it's accessing the table with a wrong name. My application is called "helloworld" since it's a test, and instead of accessing to mydb.AERODROMES, it is accessing to mydb.helloworld_aerodrome (Also note the case sensitive problem).

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

Since I already had the database populated, I haven't run syncdb (I understood it wasn't neccessary, but maybe this is the problem).

所以,问题是我不知道为什么要在表名中添加"helloworld_",而且我仍然不确定我到底在哪里固定表名(从那里来具有机场"而不是机场"的区分大小写的问题.

So, the problem is that I don't know why it is adding "helloworld_" to the table name, and also that I still don't know for sure where exactly am I fixing the table name (and from there comes the case sensitive problem having "aerodrome" and not "AERODROMES").

这里有帮助吗?

推荐答案

使用 Meta 类(

Use the Meta class (documentation here) inside your models.py model definition:

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数据库中模型表的默认命名方案.

This will override the default naming scheme for model tables in the SQL database.

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

You can also add the managed attribute to control whether or not python manage.py syncdb and python manage.py flush manage the table.

class Aerodrome(models.Model):
    # ...

    class Meta:
        db_table = 'AERODROMES'
        managed = False

通过此操作,您可以 syncdb ,而不必担心会擦除数据.

With this you can syncdb without fear of wiping your data.

这篇关于Django的数据库表名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆