models.py
class Author(models.Model):
name = models.CharField(max_length=30)
surname = models.CharField(max_length=50)
class Exhibit(models.Model):
author_id = models.ForeignKey(Author)
title = models.CharField(max_length=60)
create.sql(PostgreSQL)
CREATE TABLE Author (
id serial NOT NULL,
name varchar(30) NOT NULL,
surname varchar(50) NOT NULL,
);
CREATE TABLE Exhibit (
id serial NOT NULL,
author_id int NOT NULL,
title varchar(50) NOT NULL
);
这些是我的简化模型(用数据库表一对一映射),我正在执行看起来像那样的原始查询
Exhibit.objects.raw("SELECT * FROM Exhibit WHERE author_id = 1")
结果,我得到一个ValueError - 无法分配" 1":" Exhibit.author_id"必须是"作者"实例。
有没有办法解决这个问题而不使用ORM和Author.objects(以及类似的东西),只需查询数据库?
答案 0 :(得分:2)
state = colorado
答案 1 :(得分:0)
看看ORM sql查询,也许这有帮助
x = Exhibit.objects.filter(author_id=x)
print x.query
#check-up
x2=Exhibit.objects.raw(str(x.query))