我在 MySQL
中有一个名为 main_A
的表,有 2 列。
在我的 models.py
中,我有:
from django.db import models
class main_A(models.Model):
x= models.IntegerField(primary_key = True)
y= models.IntegerField(blank = True)
我想从 django shell 查看它的行。 注意:数据库中已存在表。
这是我的项目文件的 View : http://s18.postimg.org/musyd3pe1/1234.png
我正在对 django 进行统计:
python manage.py shell
现在...我应该写:
from models import main_A
但是说:
ImportError: NO module named models
只有我这样写才能通过这个错误:
from main.models import main_A
那么我可以这样做:
p1 = main_A.objects.all()
p1
但它告诉我:
table main_main_A doesn't exists
我知道 django 自动将包的名称添加到所有表中...这就是为什么我得到 main_main_A
但我不明白为什么 from models import main_A
不起作用。
我怎样才能通过这个?
最佳答案
如果数据库中的表名为 main_A
,那么在 Django models.py
中,您应该将类定义为 A
而不是 main_A
:
from django.db import models
class A(models.Model):
x= models.IntegerField(primary_key = True)
y= models.IntegerField(blank = True)
然后在 shell 中:
from main.models import A
p1 = A.objects.all()
p1
关于python - 使用 django shell 访问表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32421228/