Using a variable as a Model Lookup
我正在向视图传递一个变量,它是要查询的模型的名称。
1 2 | model_name = 'application' assets = model_name.objects.all() |
我得到的错误是unicode对象没有
我认为这与*args和**kwargs(我是新手,但我想我是新手)有关,特别是在我的代码中的其他地方,我有:
1 2 3 | role_set = ['primary_tech', 'primary_biz', 'backup_tech', 'backup_biz'] for role in role_set: records_to_change = Item.objects.filter(**{role:old_owner}) |
很好用。我尝试了我能想到的*和**的每一种组合,并且为了一致性而将它包装在一个
python 2.7,django 1.5版
Traceback:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 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) |
1 2 3 4 5 6 7 8 9 10 11 | 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>, ... ] |