Comments (1)
If you know which columns you are selecting in the first place, then
you know which columns you're iterating already! Can you just pass
that list into your template alongside any query? If you're looking
for where in the datastructures this information resides, you want to
look at QueryResultWrapper.query_meta and
QueryResultWrapper.column_meta. You get a QueryResultWrapper by
calling SelectQuery.execute() which happens implicitly when iterating
a select query.
Charlie
On Wed, Jan 4, 2012 at 9:12 PM, David Watson
[email protected]
wrote:
I have a model where I do something simple:
class Whatever(db.Model):
field1 = CharField()
field2 = CharField()
field3 = CharField()but in my view I'd like the query to behave like a SQL select. That is, I only get the columns back that I asked for in the select:
whatevers = Whatever.select(Whatever: ['id', "field2"]}) # at this point id and field2 are populated
columns=whatever.model._meta.get_field_names() # this gets me all the fields, not the fields matching the previous selectWhat I'm wondering is if there's a way to get the columns to match whatever's requested in the query. Obviously, I could do a list comprehension or the like to reduce the column list to the result set, but I'm looking for a more architectural solution - one that's supported within the model. It seemed like there would be something like your ModelConverter's only or exclude parameters, but I couldn't find an example where the select and the column list were matched in this way.
The use case here is simple: I want to be able to generate tables of data in my templates like:
{% for column in columns %} {% endfor %} {% for whatever in whatevers %} {% for column in columns %}{% endfor %} {% endfor %}
{{ column }} {% if loop.index == 1: %}{{ whatever[column] }}{% else %}{{ whatever[column] }}{% endif %} Thanks!
Reply to this email directly or view it on GitHub:
#40
from peewee.
Related Issues (20)
- Regression of < used on connections HOT 4
- Correct way to migrate a table to update a ForeignKeyConstraint with `ON DELETE CASCADE` HOT 1
- the count is diffrent HOT 3
- Bind schema - similar to how we can bind database to a model HOT 2
- Subquery inside CASE...WHEN... should not have alias added HOT 2
- Deleting items from table with JOIN in a query HOT 5
- TypeError: 'str' object is not callable peewee HOT 1
- Handle connection in Sanic framework HOT 3
- pwiz generates invalid Python code when creating BareField instances HOT 5
- Reuse Mysql Connection Exception HOT 2
- Joining From Different Database throwing an error HOT 1
- create_tables() does not create any fields HOT 2
- pyproject.toml file can't build peewee HOT 5
- Unexpected argument 'named_cursor' after upgrade to 3.17.6 HOT 3
- bulk update uuid PostgreSQL HOT 1
- Check Constraint not recognized in Pewee but in SQLITE3 HOT 4
- 可不可以支持开发sqlserver? HOT 2
- sql in record update HOT 6
- Database as single source of truth HOT 4
- When joining two tables using a foreign key, the query result for the foreign key field does not meet expectations.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from peewee.