Thanks for the help Stephen. On Wednesday, April 15, 2015 at 11:53:09 AM UTC-5, Stephen Butler wrote: > > Or, if you know all the Model classes are in the same module that > you've imported, it should be as easy as: > > from myapp import models > > model_class = getattr(models, tableName) > > On Wed, Apr 15, 2015 at 11:50 AM, Stephen J. Butler > <stephen...@gmail.com <javascript:>> wrote: > > Classes (MyModel) are attributes of the module (myapp.models). No > > reason to use eval. You can get the module from importlib and then the > > class from using getattr() on the module. > > > > > http://stackoverflow.com/questions/10773348/get-python-class-object-from-string > > > > > > > > > On Wed, Apr 15, 2015 at 11:45 AM, Henry Versemann <fence...@gmail.com > <javascript:>> wrote: > >> I have an incoming list of DB table names associated with my > application. I > >> want to iterate through this list of table names getting all of the > entries > >> for each table and then print some data from each tables' entry to a > file. > >> I've tried to write this so I can use it for as many app-associated > tables > >> as possible. > >> > >> My problem is since the logic won't know which tables will be in the > >> incoming list I need to try to reference the entries in each table > using > >> some kind of evaluated version of a variable containing the name of > each > >> table, as I iterate through the list. > >> > >> I'm sure I could do this using an "eval" statement like this > >> > >> tblComannd = "tblEntryLst = " + str(tableName) + ".objects.all()" > >> > >> eval(tblComannd) > >> > >> but I also know that the "eval" statement is not the safest thing to > use. > >> > >> So far I've tried to reference the table name in the following ways: > >> > >> tblEntryLst = str(tableName).objects.all() > >> tblEntryLst = tableName.objects.all() > >> > >> with no success at all and only an AttributeError exception being the > >> result. > >> > >> I think its possible since I've successfully done some similar things > with > >> referencing and saving the values of unknown keys for dictionaries > similar > >> to this: > >> > >> new_dictionary[str(extracted_key)] = old_dictionary[str(extracted_key)] > >> > >> but don't know this for sure. > >> > >> Can someone please confirm if this is indeed possible to do or not, and > if > >> so give a general format or example for how to do it? > >> > >> Any help would be much appreciated. > >> > >> Thanks. > >> > >> Henry > >> > >> > >> -- > >> You received this message because you are subscribed to the Google > Groups > >> "Django users" group. > >> To unsubscribe from this group and stop receiving emails from it, send > an > >> email to django-users...@googlegroups.com <javascript:>. > >> To post to this group, send email to django...@googlegroups.com > <javascript:>. > >> Visit this group at http://groups.google.com/group/django-users. > >> To view this discussion on the web visit > >> > https://groups.google.com/d/msgid/django-users/e3430efb-7fc2-4baf-931b-6ec8d8580315%40googlegroups.com. > > > >> For more options, visit https://groups.google.com/d/optout. >
-- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/83866de5-07b8-4b1a-85ef-3b1993539494%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.