Django编程错误关系不存在

时间:2016-03-02 17:57:52

标签: django django-models

我的应用程序中的所有内容似乎都运行得很顺利,直到我尝试从管理站点访问这些模型,当我收到我提到的错误时,我安装了Allauth并进行了所有必要的迁移,但我仍然保持相同无论我做什么都会出错。

有什么想法吗?

以下是模型:

class Cuentas(models.Model):
    idCuenta = models.AutoField(primary_key=True, null=False, max_length=15)
    user = models.OneToOneField(User)
    Saldo = models.CharField(max_length=50)

    class Meta:
        managed = False
        db_table = 'cuentas'

class Depositos(models.Model):
    idDeposito = models.AutoField(primary_key=True, null=False, max_length=15)
    idCuenta = models.ForeignKey(Cuentas)
    Tipo = models.CharField(max_length=200)
    Monto = models.CharField(max_length=50)
    Fecha = models.DateField()

    class Meta:
        managed = False
        db_table = 'deposits'

回溯:

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in wrapper
  583.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  105.                     response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  52.         response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py" in inner
  206.             return view(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapper
  29.             return bound_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  105.                     response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in bound_func
  25.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in changelist_view
  1485.                 self.list_max_show_all, self.list_editable, self)
File "/Library/Python/2.7/site-packages/django/contrib/admin/views/main.py" in __init__
  110.         self.get_results(request)
File "/Library/Python/2.7/site-packages/django/contrib/admin/views/main.py" in get_results
  219.         result_count = paginator.count
File "/Library/Python/2.7/site-packages/django/core/paginator.py" in _get_count
  72.                 self._count = self.object_list.count()
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in count
  338.         return self.query.get_count(using=self.db)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in get_count
  436.         number = obj.get_aggregation(using=using)[None]
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in get_aggregation
  402.         result = query.get_compiler(using).execute_sql(SINGLE)
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  786.             cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  81.             return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  65.                 return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/utils.py" in __exit__
  94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  65.                 return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /admin/DraftFantasy/cuentas/
Exception Value: relation "cuentas" does not exist
LINE 1: SELECT COUNT(*) FROM "cuentas"

这是初始迁移中的代码,它创建了两个模型

class Migration(migrations.Migration):

dependencies = [
    migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
    migrations.CreateModel(
        name='Cuentas',
        fields=[
            ('idCuenta', models.AutoField(max_length=15, serialize=False, primary_key=True)),
            ('Saldo', models.CharField(max_length=50)),
            ('user', models.OneToOneField(to=settings.AUTH_USER_MODEL)),
        ],
        options={
        },
        bases=(models.Model,),
    ),
    migrations.CreateModel(
        name='Depositos',
        fields=[
            ('idDeposito', models.AutoField(max_length=15, serialize=False, primary_key=True)),
            ('Tipo', models.CharField(max_length=200)),
            ('Monto', models.CharField(max_length=50)),
            ('Fecha', models.DateField()),
            ('idCuenta', models.ForeignKey(to='DraftFantasy.Cuentas')),
        ],
        options={
        },
        bases=(models.Model,),
    ),

谢谢!

1 个答案:

答案 0 :(得分:0)

我不确定,这是否是同一个问题,但我只是通过捕获此ProgrammingError例外来解决我自己的类似问题 - 看看here

根据我从调查中得知,似乎在第一次运行python manage.py migrate之前,admin.py中的配置已加载。这在开发过程中可能会非常棘手,因为如果您首先创建模型,在本地迁移模型以及稍后在admin.py中进行一些取决于新模型的更改,您就不会注意到这个问题。

因此,请确保您的admin.py文件不依赖于模型,或者能够在迁移数据库之前首次运行