Giter Club home page Giter Club logo

Comments (1)

coleifer avatar coleifer commented on August 16, 2024

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

{% 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.