gpt4 book ai didi

python - 使用变量作为模型查找

转载 作者:太空宇宙 更新时间:2023-11-03 18:34:58 26 4
gpt4 key购买 nike

我正在向我的 View 传递一个变量,它是要查询的模型的名称。

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

我收到 unicode 对象没有 objects 属性的错误,这是有道理的,因为我的调试器按预期显示 model_name = u'application' (不符合预期) )。

我认为这与 *args 和 **kwargs 有关(我是新手,但我认为我明白了),特别是因为我的代码中的其他地方有:

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

效果很好。我尝试了我能想到的所有 * 和 ** 组合,并为了保持一致性将其包装在 for model_name in [model_name] 中,但一切都给我带来了语法错误。我错过了什么?

Python 2.7、Django 1.5

回溯:

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)

最佳答案

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>, ... ]

关于python - 使用变量作为模型查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21758503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com