thumbtackthief
thumbtackthief

Reputation: 6211

Using a variable as a Model Lookup

I am passing in a variable to my view which is the name of the model to be queried against.

model_name = 'application'
assets = model_name.objects.all()

I get the error that unicode objects don't have objects properties, which makes sense as my debugger shows model_name = u'application' as expected (not as wanted).

I figure it has to do with *args and **kwargs (which I'm new to, but think I get) especially since elsewhere in my code I have:

role_set = ['primary_tech', 'primary_biz', 'backup_tech', 'backup_biz']
   for role in role_set:
       records_to_change = Item.objects.filter(**{role:old_owner})

which works fine. I tried every combination of * and ** I could think of, as well as wrapping it in a for model_name in [model_name] for consistency's sake, and everything gives me a syntax error. What am I missing?

Python 2.7, Django 1.5

Traceback:

Environment:


Request Method: GET
Request URL: http://localhost:8000/application/all/

Django Version: 1.6.1
Python Version: 2.7.2
Installed Applications:
('suit',
 'south',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.redirects',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'rest_framework',
 'ldap_sync',
 'crispy_forms',
 'ownership.apps.Catalog',
 'ownership.apps.Assets',
 'ownership.apps.Shared',
 'ownership.libs.display',
 'django_tables2',
 'haystack',
 'autocomplete_light',
 'reversion',
 'debug_toolbar')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.contrib.redirects.middleware.RedirectFallbackMiddleware',
 'django.middleware.transaction.TransactionMiddleware',
 'reversion.middleware.RevisionMiddleware',
 'ownership.libs.shibboleth.CustomHeaderMiddleware',
 'ownership.libs.middleware.LoginRequiredMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')


Traceback:
File "/Users/nicholsp/.virtualenvs/ownership/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  90.                 response = middleware_method(request)
File "/Users/nicholsp/.virtualenvs/ownership/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  40.         __import__(name)
File "/Users/nicholsp/code/ownership/ownership/urls.py" in <module>
  27.     url(r'^', include('ownership.apps.Assets.urls'), name='home'),
File "/Users/nicholsp/.virtualenvs/ownership/lib/python2.7/site-packages/django/conf/urls/__init__.py" in include
  26.         urlconf_module = import_module(urlconf_module)
File "/Users/nicholsp/.virtualenvs/ownership/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  40.         __import__(name)
File "/Users/nicholsp/code/ownership/ownership/apps/Assets/urls.py" in <module>
  3. import views

Exception Type: SyntaxError at /application/all/
Exception Value: invalid syntax (views.py, line 132)

Upvotes: 1

Views: 114

Answers (1)

Anentropic
Anentropic

Reputation: 33823

from django.db.models import get_model

class MyModel(models.Model):
   ...

model_class = get_model('myapp', 'mymodel')
print model_class.__name__
'MyModel'

model_class.objects.all()
[<MyModel: 1>, <MyModel: 2>, <MyModel: 3>, ... ]

Upvotes: 1

Related Questions