eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am having trouble in importing an XML file to Pandas where I need to grab data from two parent nodes. One parent node (AgentID) has data directly in it, and the other (Sales) has child nodes (Location, Size, Status) that contain data, as given below. test_xml = '''<TEST_XML> <Sales> <AgentID>0001</AgentID> <Sale> <Location>0</Location> <Size>1000</Size> <Status>Available</Status> </Sale> <Sale> <Location>1</Location> <Size>500</Size> <Status>Unavailable</Status> </Sale> </Sales> </TEST_XML>'''  when I try to import this to a Pandas Dataframe below is the only way I was able to grab data under the Sale tag. import pandas as pd df = pd.read_xml(test_xml, xpath='//Sale')  which gives me a dataframe like the one below:  Location Size Status 0 0 1000 Available 1 1 500 Unavailable  What I need is including the AgentID tag in the DataFrame too, to get the following, but I was unsuccessful. Expected output is given below for clarity:  AgentID Location Size Status 0 0001 0 1000 Available 1 0001 1 500 Unavailable  Is there a way to manipulate the xpath parameter to include the data inside the AgentID tag as well, or is it impossible to do it using Pandas' read_xml function? I tried passing a list like xpath=['//AgentID', '//Sale'] but of course, it did not work...

Answer: I don't think you can get the desired output using just read_xml(); however, it's possible by manipulating it a bit. Essentially, the idea is to get everything from the xml using a generic xpath, select the required columns, populate the AgentID column to corresponding to Sale columns; then remove redundant rows. df = pd.read_xml(io.StringIO(test_xml), xpath='//*', dtype=str)[['AgentID', 'Location', 'Size', 'Status']] df['AgentID'] = df['AgentID'].ffill() df = df.dropna(how='any').astype({'Location': int, 'Size': int}).reset_index(drop=True)    An "easier" solution to get a parent node (although not related to the exact question in the OP) is to convert the XML into a Python dictionary and normalize it into dataframe using pd.json_normalize. This works because meta fields (in this case AgentID) can be specified here. However, we need to install a third-party library (xmltodict) to achieve the first step. !pip install xmltodict import xmltodict df = ( pd.json_normalize(xmltodict.parse(test_xml)['TEST_XML']['Sales'], record_path=['Sale'], meta=['AgentID']) [['AgentID', 'Location', 'Size', 'Status']] )
Question: I have two dataframes. df1:  Date High Mid Low 1 2023-08-03 00:00:00 29249.8 29136.6 29152.3 4 2023-08-03 12:00:00 29395.8 29228.1 29105.0 10 2023-08-04 12:00:00 29305.2 29250.1 29137.1 13 2023-08-05 00:00:00 29099.9 29045.3 29073.0 18 2023-08-05 20:00:00 29061.6 29047.1 29044.0 .. ... ... ... ... 696 2023-11-26 20:00:00 37732.1 37469.9 37370.0 703 2023-11-28 00:00:00 37341.4 37138.2 37254.1 707 2023-11-28 16:00:00 38390.7 38137.2 37534.4 711 2023-11-29 08:00:00 38419.0 38136.3 38112.0 716 2023-11-30 04:00:00 38148.9 37800.1 38040.0  and df2:  Start Top Bottom 0 2023-11-28 00:00:00 37341.4 37138.2 1 2023-11-24 12:00:00 38432.9 37894.4  I need to check if the values in the first dataframe fall within the range of the values in a row of a second dataframe and store the number of matches in a column. I can do it using iteration like this: for idx in df1.index: df2.loc[ (df2.Start != df1.at[idx, 'Date']) & (df2.Bottom < df1.at[idx, 'High']) & (df2.Top > df1.loc[idx, ['Mid', 'Low']].max()), 'Match'] += 1  But this way is slow. Is there a faster way to do it without iteration?

Answer: If you have enough memory (depends on df1 and df2), you can use a cross merge: df2['Match'] = (df2.reset_index() .merge(df1, how='cross') .loc[lambda x: (x.Start != x.Date) & (x.Bottom < x.High) & (x.Top > x[['Mid', 'Low']].max(axis=1))] .value_counts('index').reindex(df2.index, fill_value=0))  Output: >>> df2 Start Top Bottom Match 0 2023-11-28 00:00:00 37341.4 37138.2 0 1 2023-11-24 12:00:00 38432.9 37894.4 3
Question: Consider the following code: class A: def __getitem__(self, key): print(key) a = A() a[*(1,2,3)]  With python 3.10.6, I get a SyntaxError : invalid syntax at the starred unpacking on the last line. On python 3.11.0, however, the code works fine and prints (1,2,3), as one could maybe expect. As far as I can tell, there is no difference in the language reference between the two versions regarding the syntax of subscriptions (sections 6.3.2 of the references are identical), and no mention of this change appears in the 3.11 changelog. Also, looking at these reference the behavior of 3.10.6 seem consistent (the expression between brackets should be an expression_list, which does not allow for starred unpacking). Is this a bug in the 3.11 version, or is this syntax change documented somewhere?

Answer: It appears that this change was introduced in 3.11 as a part of grammar changes for PEP 646. Relevant quote:  To put it another way, note that x[..., *a, ...] produces the same result as x[(..., *a, ...)] (with any slices i:j in ... replaced with slice(i, j), with the one edge case that x[*a] becomes x[(*a,)]).  The relevant PR appears to be bpo-43224 (#31018). The changeset was bpo-43224 (#87390).  That this is an implication of PEP 646 is not obvious to me (until you read around a bit). PEP 637 was rejected but it appears to deal with this more directly. In the rejection discussion for PEP 637, Guido van Rossum says:  Let me clarify what the typing-sig folks wanted out of this PEP. We only care about adding support for x[*y] (including things like x[a, *b, c]). We'll just update PEP 646 to add that explicitly there and hope that PEP 646 fares better than PEP 637.  One last bit of trivia: I found this by looking through the pyright repo, which initially added this support in response to PEP 637 (commit). It appears that this was subsequently updated to reflect the actual changes that went into 3.11 (commit).
Question: Im trying to login with steam on https://buff.163.com. My current code looks like this. from playwright.sync_api import sync_playwright with sync_playwright() as p: browser = p.chromium.launch(headless=False, slow_mo=50) page = browser.new_page() context = browser.new_context() page.goto("https://buff.163.com/market/csgo#tab=selling&page_num=1") page.locator("xpath=/html/body/div[1]/div/div[3]/ul/li/a").click() popup = page.wait_for_event("popup") page.get_by_text("Other login methods").click()  After it clicks the last button a new windows opens. How can i acces elements in there?

Answer: Your sequence seems to be a bit out of order. There's a modal after clicking the Login button, then a popup after clicking Other login methods. Try this flow: from playwright.sync_api import sync_playwright # 1.37.0 with sync_playwright() as p: browser = p.chromium.launch(headless=False, slow_mo=50) page = browser.new_page() page.goto("<Your URL>") page.get_by_role("link", name="Login/Register").click() with page.expect_popup() as popup_info: page.get_by_text("Other login methods").click() popup = popup_info.value popup.wait_for_load_state() # interact with the popup... print(popup.title())
Question: I have two dataframes. One records the date of trades: trade = pd.DataFrame({'date': ['2019-08-31', '2019-09-01', '2019-09-04'], 'person': [1, 1, 2], 'code': [123, 123, 456], 'value1': [1, 2, 3]})  And the other records the dates of browsing history: view = pd.DataFrame({'date': ['2019-08-29', '2019-08-29', '2019-08-30', '2019-08-31', '2019-09-01', '2019-09-01', '2019-09-01', '2019-09-02', '2019-09-03'], 'person': [1, 1, 1, 2, 1, 2, 2, 1, 2], 'code': [123, 456, 123, 456, 123, 123, 456, 123, 456], 'value': [1, 2, 3, 4, 5, 6, 7, 8, 9] })  My desired outcome is to generate a list of the related browsing history (occurred within 7 days prior to the trade) for each trade. out = pd.DataFrame({'date': ['2019-08-31', '2019-09-01', '2019-09-04'], 'person': [1, 1, 2], 'code': [123, 123, 456], 'value1': [1, 2, 3], 'view_dates': [['2019-08-29', '2019-08-29'], ['2019-08-29', '2019-08-30', '2019-09-01'], ['2019-08-31', '2019-09-01', '2019-09-03']], 'view_values':[[1, 3], [1, 3, 5], [4, 7, 9]]})  I tried to use merge_asof to match the views with the trades. And then generate the list for each trade based on the merged outcome. merge_out = pd.merge_asof(view, trade, tolerance=pd.Timedelta('7d'), direction='forward', on='date', by = ['person', 'code'])  However, merge_asof uses each view entry only once. It assigns views [1, 3] to trade 1, and views [5] to trade 2. I want to assign views [1, 3] to trade 1, and views [1, 3, 5] to trade 2. I am wondering whether there is a way to identify all the items in a time range in view for each trade. Thanks!

Answer: There is no highly efficient way to do this in pure pandas, you can however use janitor's conditional_join with a helper column, then groupby.agg: import janitor trade['date'] = pd.to_datetime(trade['date']) view['date'] = pd.to_datetime(view['date']) out = (trade .assign(start_date=lambda d: d['date'].sub(pd.DateOffset(days=7))) .conditional_join(view.rename(columns={'date': 'view_dates', 'value': 'view_values'}), ('start_date', 'view_dates', '<='), ('date', 'view_dates', '>='), ('person', 'person', '=='), ('code', 'code', '=='), right_columns=['view_dates', 'view_values'] ) .drop(columns='start_date') .assign(view_dates=lambda d: d['view_dates'].dt.strftime('%Y-%m-%d')) .groupby(list(trade), as_index=False).agg(list) )  A pure pandas solution would be less efficient as one would need to merge all combinations of the same person/code: out = (trade .merge(view.rename(columns={'date': 'view_dates', 'value': 'view_values'}), on=['person', 'code']) .loc[lambda d: d['date'].gt(d['view_dates']) & d['date'].sub(pd.DateOffset(days=7)).le(d['view_dates']) ] .assign(view_dates=lambda d: d['view_dates'].dt.strftime('%Y-%m-%d')) .groupby(list(trade), as_index=False).agg(list) )  Output:  date person code value1 view_dates view_values 0 2019-08-31 1 123 1 [2019-08-29, 2019-08-30] [1, 3] 1 2019-09-01 1 123 2 [2019-08-29, 2019-08-30, 2019-09-01] [1, 3, 5] 2 2019-09-04 2 456 3 [2019-08-31, 2019-09-01, 2019-09-03] [4, 7, 9]
Question: I was going through the python assignment statement docs . Here python uses below Backus–Naur form for assignment statements. assignment_stmt ::= (target_list "=")+ (starred_expression | yield_expression) target_list ::= target ("," target)* [","] target ::= identifier | "(" [target_list] ")" | "[" [target_list] "]" | attributeref | subscription | slicing | "*" target  Where as starred_expression is in Backus-Naur Form is starred_expression ::= expression | (starred_item ",")* [starred_item] starred_item ::= assignment_expression | "*" or_expr  and yield_expression in Backus-Naur Form is yield_atom ::= "(" yield_expression ")" yield_expression ::= "yield" [expression_list | "from" expression]  After recursively going through all those related backnaur form of each sub expression given above. I am still scratching my head how does simple assignment like a=9 can fit into above back naur form. Specially how does the 9, on the RHS of the given statement can fall into yield_expression or starred_exression

Answer: Isn't it right here? starred_expression ::= expression | …  A starred_expression can be just an expression. It must be the case that expression encompasses numeric literals like 9. (Edited for clarity following comments.) UPDATE Here is the full line from starred_expression to 9. starred_expression ::= expression | (starred_item ",")* [starred_item] expression ::= conditional_expression | lambda_expr conditional_expression ::= or_test ["if" or_test "else" expression] or_test ::= and_test | or_test "or" and_test and_test ::= not_test | and_test "and" not_test not_test ::= comparison | "not" not_test comparison ::= or_expr (comp_operator or_expr)* or_expr ::= xor_expr | or_expr "|" xor_expr xor_expr ::= and_expr | xor_expr "^" and_expr and_expr ::= shift_expr | and_expr "&" shift_expr shift_expr ::= a_expr | shift_expr ("<<" | ">>") a_expr a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr m_expr ::= u_expr | m_expr "*" u_expr | m_expr "@" m_expr | m_expr "//" u_expr | m_expr "/" u_expr | m_expr "%" u_expr u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr power ::= (await_expr | primary) ["**" u_expr] primary ::= atom | attributeref | subscription | slicing | call atom ::= identifier | literal | enclosure literal ::= stringliteral | bytesliteral | integer | floatnumber | imagnumber integer ::= decinteger | bininteger | octinteger | hexinteger decinteger ::= nonzerodigit (["_"] digit)* | "0"+ (["_"] "0")* nonzerodigit ::= "1"..."9"  What makes it confusing is that for every element from conditional_expression down to power, the thing that makes it look like the "thing it is" is optional! For instance, in power, the ** operator is actually not even required. So we think of 2**16 as a power, but 2 also qualifies as a power. Similarly for or_test, an or keyword is not actually required. It works like that all the way up. For every line, 9 satisfies the simplest version of the syntactic element with none of the optional parts included.
Question: I want to just write a hello world with selenium and have the following code: from selenium import webdriver driver = webdriver.Chrome('C:/Users/[...]/chromedriver/chromedriver.exe') driver.get("https://www.google.com")  But I keep getting the following error: Traceback (most recent call last): File "C:\Users\Username\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 38, in get_path path = SeleniumManager().driver_location(options) if path is None else path ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Username\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 75, in driver_location browser = options.capabilities["browserName"] ^^^^^^^^^^^^^^^^^^^^ AttributeError: 'str' object has no attribute 'capabilities' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:\Users\[...]\ets.py", line 3, in <module> driver = webdriver.Chrome('C:/Users/[...]/chromedriver/chromedriver.exe') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Username\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__ super().__init__( File "C:\Users\Username\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 51, in __init__ self.service.path = DriverFinder.get_path(self.service, options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Username\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 40, in get_path msg = f"Unable to obtain driver for {options.capabilities['browserName']} using Selenium Manager." ^^^^^^^^^^^^^^^^^^^^  I am using Chrome version: 119.0.6045.200 And ChromeDriver: 119.0.6045.105 I cant find anything on it, thanks for the help

Answer: You can simply get the driver by: from selenium import webdriver driver = webdriver.Chrome()  then: driver.get("https://www.google.com")  You don't need driver.exe anymore doc: https://www.selenium.dev/blog/2022/introducing-selenium-manager/
Question: I have the follwing anscombe data in long format import seaborn as sns # Load the example dataset for Anscombe's quartet anscombe_long = sns.load_dataset("anscombe")  anscombe_long  dataset x y 0 I 10.0 8.04 1 I 8.0 6.95 2 I 13.0 7.58 3 I 9.0 8.81 4 I 11.0 8.33 5 I 14.0 9.96 6 I 6.0 7.24 ... ...  I wanted to convert this into wide format where the column names x and y should have suffixes of the set number. expected output: anscombe_wide #> x1 x2 x3 x4 y1 y2 y3 y4 #> 1 10 10 10 8 8.04 9.14 7.46 6.58 #> 2 8 8 8 8 6.95 8.14 6.77 5.76 #> 3 13 13 13 8 7.58 8.74 12.74 7.71 #> 4 9 9 9 8 8.81 8.77 7.11 8.84 #> 5 11 11 11 8 8.33 9.26 7.81 8.47 #> 6 14 14 14 8 9.96 8.10 8.84 7.04 #> 7 6 6 6 8 7.24 6.13 6.08 5.25 #> 8 4 4 4 19 4.26 3.10 5.39 12.50 #> 9 12 12 12 8 10.84 9.13 8.15 5.56 #> 10 7 7 7 8 4.82 7.26 6.42 7.91 #> 11 5 5 5 8 5.68 4.74 5.73 6.89 

Answer: Use GroupBy.cumcount with DataFrame.pivot: out = (anscombe_long.assign(g = anscombe_long.groupby('dataset').cumcount()) .pivot(index='g', columns='dataset')) print (out) x y dataset I II III IV I II III IV g 0 10.0 10.0 10.0 8.0 8.04 9.14 7.46 6.58 1 8.0 8.0 8.0 8.0 6.95 8.14 6.77 5.76 2 13.0 13.0 13.0 8.0 7.58 8.74 12.74 7.71 3 9.0 9.0 9.0 8.0 8.81 8.77 7.11 8.84 4 11.0 11.0 11.0 8.0 8.33 9.26 7.81 8.47 5 14.0 14.0 14.0 8.0 9.96 8.10 8.84 7.04 6 6.0 6.0 6.0 8.0 7.24 6.13 6.08 5.25 7 4.0 4.0 4.0 19.0 4.26 3.10 5.39 12.50 8 12.0 12.0 12.0 8.0 10.84 9.13 8.15 5.56 9 7.0 7.0 7.0 8.0 4.82 7.26 6.42 7.91 10 5.0 5.0 5.0 8.0 5.68 4.74 5.73 6.89  Then convert roman numbers to integers in list comprehension: #pip install roman import roman out.columns=[f'{a}{roman.fromRoman(b)}' for a, b in out.columns] print (out) x1 x2 x3 x4 y1 y2 y3 y4 g 0 10.0 10.0 10.0 8.0 8.04 9.14 7.46 6.58 1 8.0 8.0 8.0 8.0 6.95 8.14 6.77 5.76 2 13.0 13.0 13.0 8.0 7.58 8.74 12.74 7.71 3 9.0 9.0 9.0 8.0 8.81 8.77 7.11 8.84 4 11.0 11.0 11.0 8.0 8.33 9.26 7.81 8.47 5 14.0 14.0 14.0 8.0 9.96 8.10 8.84 7.04 6 6.0 6.0 6.0 8.0 7.24 6.13 6.08 5.25 7 4.0 4.0 4.0 19.0 4.26 3.10 5.39 12.50 8 12.0 12.0 12.0 8.0 10.84 9.13 8.15 5.56 9 7.0 7.0 7.0 8.0 4.82 7.26 6.42 7.91 10 5.0 5.0 5.0 8.0 5.68 4.74 5.73 6.89  Solution if always know number of datesets by mapping dictionary: d = {'I':1, 'II':2, 'III':3, 'IV':4} out.columns=[f'{a}{d[b]}' for a, b in out.columns] print (out) x1 x2 x3 x4 y1 y2 y3 y4 g 0 10.0 10.0 10.0 8.0 8.04 9.14 7.46 6.58 1 8.0 8.0 8.0 8.0 6.95 8.14 6.77 5.76 2 13.0 13.0 13.0 8.0 7.58 8.74 12.74 7.71 3 9.0 9.0 9.0 8.0 8.81 8.77 7.11 8.84 4 11.0 11.0 11.0 8.0 8.33 9.26 7.81 8.47 5 14.0 14.0 14.0 8.0 9.96 8.10 8.84 7.04 6 6.0 6.0 6.0 8.0 7.24 6.13 6.08 5.25 7 4.0 4.0 4.0 19.0 4.26 3.10 5.39 12.50 8 12.0 12.0 12.0 8.0 10.84 9.13 8.15 5.56 9 7.0 7.0 7.0 8.0 4.82 7.26 6.42 7.91 10 5.0 5.0 5.0 8.0 5.68 4.74 5.73 6.89
Question: I have different classes which calculate different positions. Lets say ClassX provides the function get_xpos() and ClassY therefore get_ypos() For the calculation inside ClassY in need the x_pos. I cant pass the value in the __init__ function because it changes every cycle. In C++ i would to this by passing a pointer.. Moving everything in one class is not possible for different reasons. Right now im doing it like this: class ClassY: y_pos = 0 def simulate(self, x_pos): self.calc_y(x_pos)  every class has a simulate function to calculate the new values and a getter for the positions main: x = ClassX() y = ClassY() while True: x.simulate() y.simulate(x.get_xpos())  Note: the classes are in different files.

Answer: You can pass a reference to an instance of the ClassX to the constructor of ClassY. Then you can access the x_pos inside the simulate method of y without passing anything. Here's an example with random x and y=2x. import random class ClassX: def __init__(self): self.x_pos = 0 def calc_x(self): self.x_pos = random.randint(0, 10) def simulate(self): self.calc_x() class ClassY: def __init__(self, x_instance): self.x_instance = x_instance self.y_pos = 0 def calc_y(self, x_pos): self.y_pos = x_pos * 2 def simulate(self): self.calc_y(self.x_instance.x_pos) if __name__ == "__main__": x = ClassX() y = ClassY(x) for i in range(10): x.simulate() y.simulate() print(f"[{x.x_pos}, {y.y_pos}]")
Question: I'm working with an API that accepts a query parameter, which selects the values the API will return. Therefore, when parsing the API response, all attributes of the Pydantic model used for validation must be optional: class InvoiceItem(BaseModel): """ Pydantic model representing an Invoice """ id: PositiveInt | None = None org: AnyHttpUrl | None = None relatedInvoice: AnyHttpUrl | None = None quantity: PositiveInt | None = None  However, when creating an object using the API, some of the attributes are required. How can I make attributes to be required in certain conditions (in Pydantic v1 it was possible to use metaclasses for this)? Examples could be to somehow parameterise the model (as it wouldn't know without external input how its being used) or to create another model InvoiceItemCreate inheriting from InvoiceItem and make the attributes required without re-defining them.

Answer: Inspired by Marks answer I ended up using something like this: Mixin generator pattern: from typing import Self from pydantic import BaseModel, model_validator def required_mixin(required_attributes: list[str | list[str]]): class SomeRequired(BaseModel): @model_validator(mode="after") def required_fields(self) -> Self: for v in required_attributes: if isinstance(v, str): if getattr(self, v) is None: raise ValueError(f"{v} attribute is required but is None") else: if not any(getattr(self, attr) is not None for attr in v): raise ValueError(f"One of {v} is required.") return self return SomeRequired  Actual class class InvoiceItem(BaseModel): """ Pydantic model representing an Invoice """ id: PositiveInt | None = None quantity: PositiveInt | None = None unitPrice: float | None = None totalPrice: float | None = None title: str | None = None description: str | None = None class InvoiceItemCreate( InvoiceItem, required_mixin(["title", "quantity", ["unitPrice", "totalPrice"]]), ): """ Pydantic model representing an Invoice """  This allows me to re-use the definitions of the actual class and create an additional model based on the old one. The model validator allows to define certain attributes that are required when using the InvoiceItemCreate model, and even allows little more complex scenarios like "either unitPrice or totalPrice is required.
Question: I have a model Post with some fields. Aside from that I have some models which have Post as a ForeignKey. Some examples are:  class ViewType(models.Model): post = models.ForeignKey( Post, on_delete=models.CASCADE, related_name="view_types", verbose_name=_("Post"), ) view = models.CharField( max_length=20, choices=VIEW_TYPE_CHOICES, verbose_name=_("View") ) ... class HeatType(models.Model): post = models.ForeignKey( Post, on_delete=models.CASCADE, related_name="heat_types", verbose_name=_("Post"), ) heat = models.CharField( max_length=30, choices=HEAT_TYPE_CHOICES, verbose_name=_("Heat") ) ...  So what I want to do here is somehow get a dictionary with all the values of those fields in my view. For example instead of doing this for all of the models that have Post as a ForeignKey: heat_type = HeatType.objects.filter(post=post) view_type = ViewType.objects.filter(post=post) dict = { "view_type": view_type.view, "heat_type": heat_type.heat, etc... }  get all the relevant related fields in one go. Is there a simpler solution for that? Or do I have to manually get all queries for each model? Thanks in advance

Answer: With some trepidation.... class Post(models.Model): def dump(self): mydict = {} for k, v in Post.__dict__.items(): # find the attributes that represent the reverse foreign keys if type(v) == ReverseManyToOneDescriptor: print(k) mydict[k] = getattr(self, k).all() print(mydict)  With that solution, each value in mydict has a list of instances, but not the values of heat and view respectively. What you might do is add a methods to HeatType and ViewType that have the same name, like so class ViewType(models.Model): def dump(self): return self.view class HeatType(models.Model): def dump(self): return self.view  Now you can dump each related object into mydict() with  if type(v) == ReverseManyToOneDescriptor: print(k) mydict[k] = [instance.dump() for instance in getattr(self, k).all()]