0

I have about 580 tables and would like to import them using a variable if possible. Say the variable is current_product. Say for example the current_product = "apples" how can i get:

from app.models import #####

to import the the apple table please? I apologize in advance if the question is not complex enough for some.

Rene Chan
  • 864
  • 1
  • 11
  • 25
  • `from app import models; getattr(models, current_product)` – deceze Mar 09 '21 at 07:58
  • hey, thank you @deceze. I get the following: getattr(models, PG_ticker) Out[34]: app.models.apples. how do I go from there please ? – Rene Chan Mar 09 '21 at 08:22
  • ‍♂️ `the_product = getattr(models, current_product)` — Then do whatever you plan on doing with `the_product`… – deceze Mar 09 '21 at 08:25
  • Sorry to bother, I usually call table with pd.DataFrame.from_records(TotalSales.objects.all().values()). I do not know how to proceed when using getattr. Could you help ? – Rene Chan Mar 09 '21 at 08:30
  • You want to emulate `from app.models import apples`, where `apples` is dynamic and based on a variable, right? So: `apples = getattr(models, 'apples')`. There. Now you're at the same point as if you just hardcoded `from app.models import apples`. Whatever you want to do with `apples` I have no idea. – deceze Mar 09 '21 at 08:33
  • Ok, thank you. this works for me: pd.DataFrame.from_records(current_product.objects.all().values()) – Rene Chan Mar 09 '21 at 08:38

0 Answers0