Using a variable as a Model Lookup - python

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)

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

Related

Django TypeError on every page

I haven't touched the code on my app in weeks but i'm suddenly getting this exact same TypeError on every page, including Admin pages.
The last thing to happen was an update on a model to change a ForeignKey field, but I don't feel the two are related
Environment:
Request Method: GET
Request URL: http://10.0.0.161:8001/admin/
Django Version: 1.9.3
Python Version: 2.7.6
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'phones',
'jobs',
'profiles',
'freelancers',
'stock',
'finance']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'profiles.middleware.UpdateLastActivityMiddleware']
Traceback:
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
174. response = self.process_exception_by_middleware(e, request)
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
172. response = response.render()
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/template/response.py" in render
160. self.content = self.rendered_content
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/template/response.py" in rendered_content
137. content = template.render(context, self._request)
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/template/backends/django.py" in render
95. return self.template.render(context)
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/template/base.py" in render
204. with context.bind_template(self):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py" in __enter__
17. return self.gen.next()
File "/Users/studioxag/Documents/repos/telephones/lib/python2.7/site-packages/django/template/context.py" in bind_template
260. updates.update(processor(self.request))
Exception Type: TypeError at /admin/
Exception Value: 'NoneType' object is not iterable
Solved it by looking through some of my own context processors. I had one function that was returning None on a request (after changing an entry and having an empty queryset as a result).

'WSGIRequest' object has no attribute 'mode' after adding easy_thumbnails context_porcessors

I'm trying to add to easy_thumbnails context processors in my settings.py and got the next error:
WSGIRequest' object has no attribute 'mode'
Context processors config:
TEMPLATES = [
{
....
'OPTIONS': {
'context_processors': [
....
'easy_thumbnails.processors.colorspace',
'easy_thumbnails.processors.autocrop',
'filer.thumbnail_processors.scale_and_crop_with_subject_location',
'easy_thumbnails.processors.filters',
],
},
},
]
Traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:10801/uk/
Django Version: 1.8.3
Python Version: 3.4.0
Installed Applications:
('djangocms_admin_style',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'cms',
'adminsortable2',
'treebeard',
'menus',
'sekizai',
'bootstrap3',
'solo',
'mptt',
'modeltranslation',
'easy_thumbnails',
'filer',
'meta',
'meta_mixin',
'djangocms_page_meta',
'djangocms_inherit',
'djangocms_picture',
'djangocms_file',
'djangocms_teaser',
'djangocms_video',
'djangocms_link',
'djangocms_snippet',
'djangocms_text_ckeditor',
'catalog')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
'cms.middleware.language.LanguageCookieMiddleware')
Traceback:
File "/home/catalog_cms/.env/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
164. response = response.render()
File "/home/catalog_cms/.env/lib/python3.4/site-packages/django/template/response.py" in render
158. self.content = self.rendered_content
File "/home/catalog_cms/.env/lib/python3.4/site-packages/django/template/response.py" in rendered_content
135. content = template.render(context, self._request)
File "/home/catalog_cms/.env/lib/python3.4/site-packages/django/template/backends/django.py" in render
74. return self.template.render(context)
File "/home/catalog_cms/.env/lib/python3.4/site-packages/django/template/base.py" in render
208. with context.bind_template(self):
File "/usr/lib/python3.4/contextlib.py" in __enter__
59. return next(self.gen)
File "/home/catalog_cms/.env/lib/python3.4/site-packages/django/template/context.py" in bind_template
241. updates.update(processor(self.request))
File "/home/catalog_cms/.env/lib/python3.4/site-packages/easy_thumbnails/processors.py" in colorspace
64. if im.mode == 'I':
Exception Type: AttributeError at /uk/
Exception Value: 'WSGIRequest' object has no attribute 'mode'
Yes I'm using latest version of Pillow and easy_thumbnails.
I made a mistake, sorry. This is not context processors, but thumbnail processors. I just add:
THUMBNAIL_PROCESSORS = (
'easy_thumbnails.processors.colorspace',
'easy_thumbnails.processors.autocrop',
'easy_thumbnails.processors.filters',
'filer.thumbnail_processors.scale_and_crop_with_subject_location',
)
Now all working.

object() takes no parameters

I have installed django admin and am getting object() takes no parameters error. I see the error is at debug_toolbar panel.py line 55. Am not sure how to debug it.
I have pasted the traceback.
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/super_admin/
Django Version: 1.6.1
Python Version: 2.7.4
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'debug_toolbar',
'django_extensions',
'apps.app1',
'apps.accounts',
'south')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware')
Traceback:
File "/home/home/environments/env1.6.2/local/lib/python2.7/site- packages/home/core/handlers/base.py" in get_response
139. response = response.render()
File "/home/home/environments/env1.6.2/local/lib/python2.7/site- packages/django/template/response.py" in render
105. self.content = self.rendered_content
File "/home/home/environments/env1.6.2/local/lib/python2.7/site-packages/django/template/response.py" in rendered_content
81. context = self.resolve_context(self.context_data)
File "/home/home/environments/env1.6.2/local/lib/python2.7/site-packages/django/template/response.py" in resolve_context
159. return RequestContext(self._request, context, current_app=self._current_app)
File "/home/home/environments/env1.6.2/local/lib/python2.7/site-packages/debug_toolbar/panels/templates/panel.py" in _request_context__init__
55. context = processor(request)`
Exception Type: TypeError at /super_admin/
Exception Value: object() takes no parameters
Any help will be highly appreciated.
EDIT
Here is my TEMPLATE_CONTEXT_PROCESSORS settings
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.request',
'django.core.context_processors.static',
'django.contrib.messages.context_processors.messages',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
You have two entries in your TEMPLATE_CONTEXT_PROCESSORS setting which are middleware, not context processors. They belong in the MIDDLEWARE_CLASSES setting instead.

Filter iexact error Model matching query does not exist

I'm trying to verify if a value exists in my database with:
q = model.objects.filter(field__iexact='1')
But this makes an error in Django, which returns this message:
DoesNotExist at /answer_survey/
Model matching query does not exist
This is the entire traceback:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/answer_survey/
Django Version: 1.6.1
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'survey',
'south',
'django_reset',
'chartkick',
'registration')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/filipeferminiano/Documents/django/panorama/survey/views.py" in answer_survey
265. s = Survey.objects.get(name=cod_survey)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/manager.py" in get
151. return self.get_queryset().get(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py" in get
307. self.model._meta.object_name)
Exception Type: DoesNotExist at /answer_survey/
Exception Value: Survey matching query does not exist.
Your error is here:
s = Survey.objects.get(name=cod_survey)
you have't records with name that contains in cod_survey variable...
Add try/except block to catch DoesNotExist exceptions:
try:
s = Survey.objects.get(name=cod_survey)
except Survey.DoesNotExist:
s = 'something else...'

Strange django output

I've installed transifex - django application. Everything looked ok, but if I run:
./manage runserver 8000
I got error message (below). Since I'm inexpedience django user supposedly it is some easy error/mistake. Unfortunately, I'm unable to discover what the problem is.
Any clue will is appreciated!
Thanks, Tomas
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.3.1
Python Version: 2.7.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.comments',
'django.contrib.contenttypes',
'django.contrib.flatpages',
'django.contrib.humanize',
'django.contrib.markup',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.sites',
'django.contrib.admindocs',
'staticfiles',
'notification',
'django_filters',
'django_sorting',
'south',
'tagging',
'tagging_autocomplete',
'pagination',
'piston',
'contact_form',
'ajax_select',
'threadedcomments',
'authority',
'haystack',
'social_auth',
'userena',
'guardian',
'easy_thumbnails',
'djcelery',
'djkombu',
'compressor',
'transifex.txcommon',
'transifex.languages',
'transifex.projects',
'transifex.releases',
'transifex.actionlog',
'transifex.txpermissions',
'transifex.teams',
'transifex.resources',
'django_addons',
'autofetch',
'charts',
'cla',
'copyright',
'datastores',
'gtranslate',
'helptext',
'jsonmap',
'locks',
'lotte',
'priorities',
'suggestions',
'timeline',
'trans_memory',
'txapps',
'txcron',
'txtestsuite',
'watches',
'webhooks',
'widgets']
Installed Middleware:
['django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django_sorting.middleware.SortingMiddleware',
'pagination.middleware.PaginationMiddleware',
'userena.middleware.UserenaLocaleMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware']
Traceback:
File "/usr/lib/python2.7/site-packages/Django-1.3.1-py2.7.egg/django/core/handlers /base.py" in get_response
101. request.path_info)
File "/usr/lib/python2.7/site-packages/Django-1.3.1-py2.7.egg/django/core/urlresolvers.py" in resolve
252. sub_match = pattern.resolve(new_path)
File "/usr/lib/python2.7/site-packages/Django-1.3.1-py2.7.egg/django /core/urlresolvers.py" in resolve
252. sub_match = pattern.resolve(new_path)
File "/usr/lib/python2.7/site-packages/Django-1.3.1-py2.7.egg/django/core/urlresolvers.py" in resolve
250. for pattern in self.url_patterns:
File "/usr/lib/python2.7/site-packages/Django-1.3.1-py2.7.egg/django/core/urlresolvers.py" in _get_url_patterns
279. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/lib/python2.7/site-packages/Django-1.3.1-py2.7.egg/django /core/urlresolvers.py" in _get_urlconf_module
274. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.7/site-packages/Django-1.3.1-py2.7.egg/django/utils/importlib.py" in import_module
35. __import__(name)
File "/usr/lib/python2.7/site-packages/transifex-1.3.0devel-py2.7.egg/transifex/addons/cla/urls.py" in <module>
2. from transifex.projects.urls import PROJECT_URL
File "/usr/lib/python2.7/site-packages/transifex-1.3.0devel-py2.7.egg/transifex/projects/urls/__init__.py" in <module>
6. from transifex.projects.feeds import LatestProjects, ProjectFeed, ProjectTimelineFeed
File "/usr/lib/python2.7/site-packages/transifex-1.3.0devel-py2.7.egg/transifex/projects/feeds.py" in <module>
11. current_site = Site.objects.get_current()
File "/usr/lib/python2.7/site-packages/Django-1.3.1-py2.7.egg/django/contrib/sites/models.py" in get_current
25. current_site = self.get(pk=sid)
File "/usr/lib/python2.7/site-packages/Django-1.3.1-py2.7.egg/django/db/models/manager.py" in get
132. return self.get_query_set().get(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/Django-1.3.1-py2.7.egg/django/db/models/query.py" in get
349. % self.model._meta.object_name)
Exception Type: DoesNotExist at /
Exception Value: Site matching query does not exist.
First of all run python manage.py syncdb. Then check this query in django shell:
Site.objects.get(id=1)
if DoesNotExist error is raised create Site object manually:
Site.objects.create(name='example.com', domain='example.com')

Categories

Resources