数据库连接 - Django是自动重命名的东西!混乱

时间:2012-05-03 01:02:42

标签: python mysql database django

制作博客,数据库等等。事实上,我有一个名为'posts'的数据库。

'default': {
    'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'posts',                      # Or path to database file if using sqlite3.
    'USER': '*******',                      # Not used with sqlite3.
    'PASSWORD': '*******',                  # Not used with sqlite3.
    'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',                      # Set to empty string for localhost. Not used with sqlite3.
    'PORT': '****',                      # Set to empty string for default. Not used with sqlite3.
}

models.py

from django.db import models


class posts(models.Model):
    author = models.CharField(max_length = 30)
    title = models.CharField(max_length = 100)
    bodytext = models.TextField()
    timestamp = models.DateTimeField()

views.py

from django.shortcuts import render_to_response
from models import posts 



#from settings import default
def index(request):
    entries = posts.objects.all()[:10]
    return render_to_response('homepage/index.html', {'posts' : entries}

输出

OperationalError at /
(1049, "Unknown database 'posts.homepage_posts'")
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.4
Exception Type: OperationalError
Exception Value:    
(1049, "Unknown database 'posts.homepage_posts'")
Exception Location: /Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg/MySQLdb/connections.py in __init__, line 187
Python Executable:  /usr/bin/python
Python Version: 2.7.1
Python Path:    
['/Users/thor/Sites/FirstBlog',
 '/Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
 '/Library/Python/2.7/site-packages']
Server time:    Wed, 2 May 2012 19:56:53 -0500

它会自动将.homepage_posts后缀为数据库名称,从而无法访问它。请帮帮忙?

1 个答案:

答案 0 :(得分:0)

自动生成表格的django默认命名约定是使用<app_label>_为表名添加前缀
您显然在名为“主页”的应用程序中有此模式/视图。这是有效的,不是您的问题。 django这样做可以在不同应用程序之间命名各种表格,并防止在第三方应用程序中混合时发生名称冲突。

此错误告诉您整个数据库不存在。它几乎100%意味着你没有告诉django从你的模型结构填充你的数据库:

python manage.py syncdb

立即运行此功能,以及每次添加新应用或创建新模型时。它将生成您的数据库和表格。

如果您真的关心,您还可以通过为模型设置meta“db_table”字段来控制数据库表的实际名称:https://docs.djangoproject.com/en/dev/ref/models/options/#db-table

class posts(models.Model):
    class Meta:
        db_table = 'foo_bar'

......但是,这又是一个单独的问题。