You can use get_model

from django.db.models.loading import get_model

YourModel = get_model('your_app', tableName)

On Wed, Apr 15, 2015 at 2:01 PM, Tim Chase <django.us...@tim.thechases.com>
wrote:

> On 2015-04-15 09:45, Henry Versemann wrote:
> > 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)
>
> Since it's just Python, you can create mappings of the table-objects,
> and then get attributes on them.
>
>   table_names = {
>     "tblA": tblA,
>     "tblB": tblB,
>     "tblRenamed": tblXYZ,
>     }
>
>   for name, cls in table_names.items():
>     log.info("Dumping %s", name)
>     for item in cls.objects.all():
>       write_to_file(item)
>
> Presumably, these classes come from some namespace like a module, so
> you can even do something like
>
>   import models
>   table_names = ["tblA", "tblB"]
>   for name in table_names:
>     log.info("Dumping %s", name)
>     cls = getattr(models, name)
>     for item in cls.objects.all():
>       write_to_file(item)
>
> -tim
>
>
>
> --
> 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/20150415120110.77c4ea6f%40bigbox.christie.dr
> .
> 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/CALn3ei33E%2BkH63sCXWx_Y7xPSa91Kv_JB4kYHZt_djvF46LUyQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to