Google App Engine python, GQL, select only one column from datastore
Im trying to pull only one column from a datastore table
I have a Books model with id, key, title, author, isbn and price
everything = db.GqlQuery('SELECT * FROM Books') giv开发者_运维知识库es me everything, but say i only want the title
books = db.GqlQuery('SELECT title FROM Books')
Ive tried everything people have suggested but nothing seems to work
Any help is much appreciated Thanks
You can't. GQL is not SQL, and the datastore is not a relational database. An entity is stored as a single serialized protocol buffer, and it's impossible to fetch part of an entity; the whole thing needs to be deserialized.
A design consideration in the datastore is to use redundancy where you wouldn't in a relational database.
So for example, your code could have a entities for "Titles" that it adds each time a book is added, and removes each time a book is removed. Then you could query the Titles entities to get all the titles without having to load all the books.
Your code has to enforce those rules, but with a bit of python abstraction, that's not difficult (i.e. put all accesses to books or titles behind a class whose methods enforce the relationships in your data).
I highly suggest reading some of the more advanced topics on the data store here.
People often refer to moments of realization as "How I learned to stop worrying and love the data store." They are basically moments when people switch out of a Normal Form way of thinking about data, and into a distributed/redundant view where you can just spray data at the data store and it'll handle it.
精彩评论