使用psycopg2访问时缺少Django模型

2024-09-27 00:19:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在构建一个django应用程序,models.py包括几个模型:

  • Customer
  • Error
  • Record

当我运行python3 manage.py shell时,我没有错误:

From myapp.models import Customer, Error, Record
records = Record.objects.all()
print(len(records))
# the same works if I run the query for Customer, Error, etc.

但是,如果我尝试使用psycopg2连接,我会发现有些表是不可访问的

cur.execute("select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';")
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
cursor.execute("select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';")
print cursor.fetchall()

[('django_migrations',),
 ('django_content_type',),
 ('django_admin_log',),
 ('auth_group_permissions',),
 ('auth_group',),
 ('auth_user_groups',),
 ('auth_permission',),
 ('auth_user_user_permissions',),
 ('django_session',),
 ('my_app_contactme',),
 ('my_app_employee',),
 ('auth_user',),
 ('Error',),
 ('my_app_blogpost_category',),
 ('Customer',),
 ('Record',)]

 cur.execute("SELECT * from Customer")

 ---------------------------------------------------------------------------
 ProgrammingError Traceback (most recent call last) <ipython-input-38-3532e0e20e34> in <module>()
 ----> 1 cur.execute("SELECT * from Customer")

 ProgrammingError: relation "customer" does not exist
 LINE 1: SELECT * from Customer

我不明白为什么CustomerErrorRecord会引起通过psycopg2访问的问题


Tags: djangofromauthexecutemyerrorcustomerrecord

热门问题