Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get all fields in related model

See original GitHub issue

Is there a way to get all the fields in a related model instead of having to explicitly follow individual relationships with the double underscore notation ('offer__provider')?

For example I have the following statement:

productapps = ProductApplication.objects.all().select_related('offer')

And I want to turn that queryset into a dataframe with all the fields from ProductApplication and Offer.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

pathwaycommented, Jul 3, 2017

This is how I solved it myself, in case it is useful to you at all.

Given class Parent, and class Child with FK to Parent:

fr = Child.objects.select_related('parent').values_list(*cols)
df=pd.DataFrame.from_records(data=list(fr),columns=cols )
  • with this approach, I had to list cols manually
  • values() did not work (it complained about not recognizing parent__field which I thought was very strange), I had to use values_list for some reason. I did not realize values and values_list should act differently in this way?
pathwaycommented, Jul 4, 2017

Yes I benefit from open source 100%, and am happy to contribute wherever reasonable!

However, no I have not benefited from this project at all yet, because it did not do what I needed, which is: Get fields in related model. I provided my solution code above, “at no cost to you” as you say.

If I saw a simple way to integrate my solution code with this project, I may be willing to do so. But at the moment, its simpler for me to just use the 3 lines above. After briefly looking at this project’s source code, its not clear to me that the approach I took in my 3 lines would be directly helpful.

Of course I wish you the best with this project! Your goal is to connect two excellent open source frameworks which is awesome.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get all fields of related object Django - Stack Overflow
To formalize the answer, the instance is actually an instance of the model the form is based on. Thus, in your case, you...
Read more >
QuerySet API reference | Django documentation
Annotates each object in the QuerySet with the provided list of query expressions. An expression may be a simple value, a reference to...
Read more >
Making queries — Django 4.1.4 documentation
The simplest way to retrieve objects from a table is to get all of them. ... To span a relationship, use the field...
Read more >
Get fields from related model - Odoo
Hi, if you need to show the car name and price together in a field, you have to define the name_get function for...
Read more >
django get all fields from a model Code Example
django get all model fields ; 1. def get_model_fields(model): ; 2. return model._meta.fields ; 3. ​.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found