Issue: is it possible to get select query columns behavior similar to only or exclude in model converter?
Author: davidthewatson
State: closed
Created: 2012-01-05 03:12:30

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 select
```

What 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:

```
<table>
<thead>
<tr>
{% for column in columns %}
<td>{{ column }}</td>
{% endfor %}
</tr>
</thead>
<tbody>
{% for whatever in whatevers %}
<tr>
 {% for column in columns %}<td>{% if loop.index == 1: %}<a href="{{ url_for('detail', id=0) }}">{{ whatever[column] }}</a>{% else %}{{ whatever[column] }}{% endif %}</td>{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
```

Thanks!


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
reply@reply.github.com
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 select
> 
> What 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:
> 
> <table>
> <thead>
> <tr>
> {% for column in columns %}
> <td>{{ column }}</td>
> {% endfor %}
> </tr>
> </thead>
> <tbody>
> {% for whatever in whatevers %}
> <tr>
> {% for column in columns %}<td>{% if loop.index == 1: %}<a href="{{ url_for('detail', id=0) }}">{{ whatever[column] }}</a>{% else %}{{ whatever[column] }}{% endif %}</td>{% endfor %}
> </tr>
> {% endfor %}
> </tbody>
> </table>
> 
> 
> Thanks!
> 
> ---
> 
> Reply to this email directly or view it on GitHub:
> https://github.com/coleifer/peewee/issues/40