【问题标题】:Django error: no such table "boletin_registrado"Django 错误:没有这样的表“boletin_registrado”
【发布时间】:2017-03-27 16:18:47
【问题描述】:

我正在开发的一个新应用程序出现此错误,该应用程序是我的登录页面内的电子邮件公告。

我在 models.py 中创建了名为“registrado”的模型(只有一个表),但是当我运行服务器时,它说没有名为“registrado”的表,但是...实际上是因为我在模型中编写了它.py 不知道为什么会出现这个错误

models.py的代码是这样的

from __future__ import unicode_literals
from django.db  import models

class registrado(models.Model):

    nombre = models.CharField(max_length = 120, blank = True, null = True)
    email = models.EmailField()
    codigo_postal = models.IntegerField()
    timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
    actualizado = models.DateTimeField(auto_now_add = False, auto_now = True)   

    def __unicode__ (self):

        return self.email

然后我修改 INSTALLED APPS 中的 settings.py 文件,将应用名称放在列表末尾

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'boletin',
]

最后,在 admin.py 中注册应用程序

from django.contrib import admin

# Register your models here.
from .models import registrado

class Adminregistrado(admin.ModelAdmin):
    list_display = ["__unicode__", "nombre", "timestamp"]
    class Meta:
        model = registrado

admin.site.register(registrado, Adminregistrado)

抱歉发了这么长的帖子,感谢您的帮助:/

【问题讨论】:

  • 表名的前缀为app。因此,对于您的情况,完整的表名将是boletin_registrado 而不是registrado,建议您使用manage.py makemigrationmanage.py migrate 来同步数据库。

标签: python django database model server


【解决方案1】:

您可以检查如下:

转到您的项目目录。

创建模型后

  1. 迁移
  2. 迁移

当您完成新的迁移文件后,您必须将它们应用到您的数据库中以确保它们按预期工作:

cd /var/opt/boletin
python manage.py makemigrations
python manage.py migrate

#You can check table in database as follows:
python manage.py dbshell 
\dt 

在这里,您将获得所有具有实际数据库名称的表。 小写使用“应用程序名和表名”生成的表名。

Application Name = "boletin"
Table Name = "registrado"

eg: Application Name + "_" + Table Name= "boletin_registrado”

在您的应用程序中,您可以按如下方式使用:

cd /var/opt/boletin
python manage.py shell 
from boletin.models import registrado or from boletin.models import *

registrado_obj = registrado.(numbre = numbre, email=email, codigo_postal=codigo_postal, timestamp=timestamp, actualizado=actualizado)
registrado_obj.save()

【讨论】:

    猜你喜欢
    • 2015-07-31
    • 2014-03-25
    • 2014-09-23
    • 2013-07-13
    • 2014-03-26
    • 2020-04-14
    • 2016-01-05
    • 2020-04-07
    • 1970-01-01
    相关资源
    最近更新 更多