Skip to content

Conversation

kevinali1
Copy link

Hey, I added a function to convert a queryset to a dataframe. I used the database query compiler instead of cursor.connection since the latter does not sanitize the sql query.

Anyways, you can merge as needed.

@kevinali1
Copy link
Author

I think your code will work fine. In fact, I'm currently using something similar to that for all my qs to df stuff.

However one place it might fail is if we have some foreign keys through select_related. For example, if I want a dataframe containing some stock price data for the last week with a column linking each price point to a stock symbol, then i think it might be difficult since values_list kinda chops of foreign keys (I think).

My code pulls the sanitized/escaped sql query from the queryset before its evaluated, then generates an iterator which is passed to the read_frame method. This is also kinda crappy since it always gets id columns, even if we use "only". But other than that, it will not break with select_related data.

Any thoughts?

@chrisdev
Copy link
Owner

Hi
Check the docs on values list v 1.4 select related. Also have a look at
django-model-utils. I can't because we've been without power for the last
two hours!!!
Ahhhh!!
Regards
Chris

Sent from my iPhone

On Aug 14, 2012, at 4:19 PM, Kevin Ali notifications@github.com wrote:

I think your code will work fine. In fact, I'm currently using something
similar to that for all my qs to df stuff.

However one place it might fail is if we have some foreign keys through
select_related. For example, if I want a dataframe containing some stock
price data for the last week with a column linking each price point to a
stock symbol, then i think it might be difficult since values_list kinda
chops of foreign keys (I think).

My code pulls the sanitized/escaped sql query from the queryset before its
evaluated, then generates an iterator which is passed to the read_frame
method. This is also kinda crappy since it always gets id columns, even if
we use "only". But other than that, it will not break with select_related
data.

Any thoughts?


Reply to this email directly or view it on
GitHubhttps://github.com//pull/1#issuecomment-7738780.

chrisdev added a commit that referenced this pull request Aug 16, 2012
Added a queryset to dataframe function
@chrisdev chrisdev merged commit d9b92f1 into chrisdev:master Aug 16, 2012
chrisdev pushed a commit that referenced this pull request May 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants