Error during Django test - python

I'm trying to run a test in Django and I keep getting this error when Django tries to create the test DB.
django.db.utils.ProgrammingError: relation "userprofile_user" does not exist
Here is my UserProfile model:
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
gender = models.CharField(max_length=140, null=True)
age_range_min = models.PositiveIntegerField(null=True)
age_range_max = models.PositiveIntegerField(null=True)
locale = models.CharField(max_length=255, null=True)
def __unicode__(self):
return u'Profile of user: %s' % self.user.email
Here is the full stack trace:
Destroying old test database 'default'...
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_from_command_line(sys.argv)
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/commands/test.py", line 30, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/commands/test.py", line 74, in execute
super(Command, self).execute(*args, **options)
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
output = self.handle(*args, **options)
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/commands/test.py", line 90, in handle
failures = test_runner.run_tests(test_labels)
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/test/runner.py", line 210, in run_tests
old_config = self.setup_databases()
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/test/runner.py", line 166, in setup_databases
**kwargs
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/test/runner.py", line 370, in setup_databases
serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/db/backends/base/creation.py", line 368, in create_test_db
test_flush=not keepdb,
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/__init__.py", line 120, in call_command
return command.execute(*args, **defaults)
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
output = self.handle(*args, **options)
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 179, in handle
created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 318, in sync_apps
cursor.execute(statement)
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/db/utils.py", line 98, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "userprofile_user" does not exist

It seems it is a problem with your migrations, try to add this to your settings in order to by pass them during tests:
class DisableMigrations(object):
def __contains__(self, item):
return True
def __getitem__(self, item):
return "notmigrations"
TESTS_IN_PROGRESS = False
if 'test' in sys.argv[1:]:
logging.disable(logging.CRITICAL)
PASSWORD_HASHERS = (
'django.contrib.auth.hashers.MD5PasswordHasher',
)
DEBUG = False
TEMPLATE_DEBUG = False
TESTS_IN_PROGRESS = True
MIGRATION_MODULES = DisableMigrations()

Related

sqlalchemy.exc.ResourceClosedError: This transaction is closed

Facing the following error in a Pyramid Application while using SQLAlchemy and Zope Transaction Manager.
This is how I am creating a scoped session:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.schema import MetaData
from sqlalchemy.orm import (
scoped_session,
sessionmaker,
)
from zope.sqlalchemy import ZopeTransactionExtension
metadata = MetaData(naming_convention=NAMING_CONVENTION)
Base = declarative_base(metadata=metadata)
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension(keep_session=True)))
I am syncing my session creation and removal with my requests in the following way:
#view_config(route_name='social_get_individual_assets', renderer='json', effective_principals=2, permission='edit')
def social_get_individual_assets(requestJson):
session = DBSession()
try:
body = requestJson.request.json_body
search_term = body['text']
horizontal = body['horizontal']
vertical = body['vertical']
log.info("social get individual assets request: %s", str(search_term).encode(encoding='utf_8'))
json_data = get_individual_assets(session, search_term, horizontal, vertical)
log.info("social get assets response: %s", str(search_term).encode(encoding='utf_8'))
transaction.commit()
DBSession.remove()
return json_data
except Exception as e:
session.rollback()
log.exception(e)
raise e
For some reason, I keep running into this error all the time:
2017-12-06 13:32:07,965 ERROR [invideoapp.views.default:465] An operation previously failed, with traceback:
File "/usr/lib64/python3.5/threading.py", line 882, in _bootstrap
self._bootstrap_inner()
File "/usr/lib64/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib64/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.5/site-packages/waitress/task.py", line 78, in handler_thread
task.service()
File "/usr/local/lib/python3.5/site-packages/waitress/channel.py", line 338, in service
task.service()
File "/usr/local/lib/python3.5/site-packages/waitress/task.py", line 169, in service
self.execute()
File "/usr/local/lib/python3.5/site-packages/waitress/task.py", line 399, in execute
app_iter = self.channel.server.application(env, start_response)
File "/usr/local/lib/python3.5/site-packages/pyramid/router.py", line 270, in __call__
response = self.execution_policy(environ, self)
File "/usr/local/lib/python3.5/site-packages/pyramid_retry/__init__.py", line 114, in retry_policy
response = router.invoke_request(request)
File "/usr/local/lib/python3.5/site-packages/pyramid/router.py", line 249, in invoke_request
response = handle_request(request)
File "/usr/local/lib/python3.5/site-packages/pyramid_tm/__init__.py", line 136, in tm_tween
response = handler(request)
File "/usr/local/lib/python3.5/site-packages/pyramid/tweens.py", line 39, in excview_tween
response = handler(request)
File "/usr/local/lib/python3.5/site-packages/pyramid/router.py", line 156, in handle_request
view_name
File "/usr/local/lib/python3.5/site-packages/pyramid/view.py", line 642, in _call_view
response = view_callable(context, request)
File "/usr/local/lib/python3.5/site-packages/pyramid/viewderivers.py", line 439, in rendered_view
result = view(context, request)
File "/usr/local/lib/python3.5/site-packages/pyramid/viewderivers.py", line 148, in _requestonly_view
response = view(request)
File "/home/ttv/invideoapp/invideoapp/views/default.py", line 148, in upload_image
transaction.commit()
File "/usr/local/lib/python3.5/site-packages/transaction/_manager.py", line 131, in commit
return self.get().commit()
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 308, in commit
t, v, tb = self._saveAndGetCommitishError()
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 301, in commit
self._commitResources()
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 446, in _commitResources
reraise(t, v, tb)
File "/usr/local/lib/python3.5/site-packages/transaction/_compat.py", line 54, in reraise
raise value
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 423, in _commitResources
rm.tpc_vote(self)
File "/usr/local/lib/python3.5/site-packages/zope/sqlalchemy/datamanager.py", line 109, in tpc_vote
self.tx.commit()
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/session.py", line 459, in commit
self._assert_active(prepared_ok=True)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/session.py", line 285, in _assert_active
raise sa_exc.ResourceClosedError(closed_msg)
sqlalchemy.exc.ResourceClosedError: This transaction is closed
Traceback (most recent call last):
File "/home/ttv/invideoapp/invideoapp/views/default.py", line 451, in update_master_json
user = getUser(session,user_id)
File "/home/ttv/invideoapp/invideoapp/invideomodules/auth_processing.py", line 12, in getUser
raise e
File "/home/ttv/invideoapp/invideoapp/invideomodules/auth_processing.py", line 8, in getUser
query = session.query(User).filter(User.user_id == userid).first()
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/query.py", line 2755, in first
ret = list(self[0:1])
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/query.py", line 2547, in __getitem__
return list(res)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/query.py", line 2855, in __iter__
return self._execute_and_instances(context)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/query.py", line 2876, in _execute_and_instances
close_with_result=True)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/query.py", line 2885, in _get_bind_args
**kw
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/query.py", line 2867, in _connection_from_session
conn = self.session.connection(**kw)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/session.py", line 966, in connection
execution_options=execution_options)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/session.py", line 971, in _connection_for_bind
engine, execution_options)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/session.py", line 417, in _connection_for_bind
self.session.dispatch.after_begin(self.session, self, conn)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/event/attr.py", line 256, in __call__
fn(*args, **kw)
File "/usr/local/lib/python3.5/site-packages/zope/sqlalchemy/datamanager.py", line 237, in after_begin
join_transaction(session, self.initial_state, self.transaction_manager, self.keep_session)
File "/usr/local/lib/python3.5/site-packages/zope/sqlalchemy/datamanager.py", line 211, in join_transaction
DataManager(session, initial_state, transaction_manager, keep_session=keep_session)
File "/usr/local/lib/python3.5/site-packages/zope/sqlalchemy/datamanager.py", line 73, in __init__
transaction_manager.get().join(self)
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 179, in join
self._prior_operation_failed() # doesn't return
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 173, in _prior_operation_failed
self._failure_traceback.getvalue())
transaction.interfaces.TransactionFailedError: An operation previously failed, with traceback:
File "/usr/lib64/python3.5/threading.py", line 882, in _bootstrap
self._bootstrap_inner()
File "/usr/lib64/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib64/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.5/site-packages/waitress/task.py", line 78, in handler_thread
task.service()
File "/usr/local/lib/python3.5/site-packages/waitress/channel.py", line 338, in service
task.service()
File "/usr/local/lib/python3.5/site-packages/waitress/task.py", line 169, in service
self.execute()
File "/usr/local/lib/python3.5/site-packages/waitress/task.py", line 399, in execute
app_iter = self.channel.server.application(env, start_response)
File "/usr/local/lib/python3.5/site-packages/pyramid/router.py", line 270, in __call__
response = self.execution_policy(environ, self)
File "/usr/local/lib/python3.5/site-packages/pyramid_retry/__init__.py", line 114, in retry_policy
response = router.invoke_request(request)
File "/usr/local/lib/python3.5/site-packages/pyramid/router.py", line 249, in invoke_request
response = handle_request(request)
File "/usr/local/lib/python3.5/site-packages/pyramid_tm/__init__.py", line 136, in tm_tween
response = handler(request)
File "/usr/local/lib/python3.5/site-packages/pyramid/tweens.py", line 39, in excview_tween
response = handler(request)
File "/usr/local/lib/python3.5/site-packages/pyramid/router.py", line 156, in handle_request
view_name
File "/usr/local/lib/python3.5/site-packages/pyramid/view.py", line 642, in _call_view
response = view_callable(context, request)
File "/usr/local/lib/python3.5/site-packages/pyramid/viewderivers.py", line 439, in rendered_view
result = view(context, request)
File "/usr/local/lib/python3.5/site-packages/pyramid/viewderivers.py", line 148, in _requestonly_view
response = view(request)
File "/home/ttv/invideoapp/invideoapp/views/default.py", line 148, in upload_image
transaction.commit()
File "/usr/local/lib/python3.5/site-packages/transaction/_manager.py", line 131, in commit
return self.get().commit()
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 308, in commit
t, v, tb = self._saveAndGetCommitishError()
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 301, in commit
self._commitResources()
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 446, in _commitResources
reraise(t, v, tb)
File "/usr/local/lib/python3.5/site-packages/transaction/_compat.py", line 54, in reraise
raise value
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 423, in _commitResources
rm.tpc_vote(self)
File "/usr/local/lib/python3.5/site-packages/zope/sqlalchemy/datamanager.py", line 109, in tpc_vote
self.tx.commit()
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/session.py", line 459, in commit
self._assert_active(prepared_ok=True)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/session.py", line 285, in _assert_active
raise sa_exc.ResourceClosedError(closed_msg)
sqlalchemy.exc.ResourceClosedError: This transaction is closed
Once I get this error, none of my Database connection stuff works. Only way out is to restart the entire application to make things work. Any idea why this is happening?
I had a similar problem, and I found using DBSession.begin_nested() solved the issue. So something like:
#view_config(route_name='social_get_individual_assets', renderer='json', effective_principals=2, permission='edit')
def social_get_individual_assets(requestJson):
try:
DBSession.begin_nested()
body = requestJson.request.json_body
search_term = body['text']
horizontal = body['horizontal']
vertical = body['vertical']
log.info("social get individual assets request: %s", str(search_term).encode(encoding='utf_8'))
json_data = get_individual_assets(session, search_term, horizontal, vertical)
log.info("social get assets response: %s", str(search_term).encode(encoding='utf_8'))
return json_data
except Exception as e:
DBSession.rollback()
log.exception(e)
raise e

Django Error: could not convert string to float in source code not in my own code

When I try run python manage.py migrate to synchronize database I get error
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 445, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.7/site-packages/django/core/management/commands/migrate.py", line 222, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "/Library/Python/2.7/site-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Library/Python/2.7/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/schema.py", line 179, in add_field
self._remake_table(model, create_fields=[field])
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/schema.py", line 77, in _remake_table
self.effective_default(field)
File "/Library/Python/2.7/site-packages/django/db/backends/base/schema.py", line 211, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
prepared=False)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 702, in get_db_prep_value
value = self.get_prep_value(value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 1804, in get_prep_value
return float(value)
ValueError: could not convert string to float: Jestem watowcem
models.py file:
# -*- coding: utf-8 -*-
from django.db import models
# Create your models here.
class ProductWithFrames(models.Manager):
def get_queryset(self):
return super(ProductWithFrames, self).get_queryset().filter(has_frame=True)
class ProductWithoutFrames(models.Manager):
def get_queryset(self):
return super(ProductWithoutFrames, self).get_queryset().filter(has_frame=False)
class Product(models.Model):
PRODUCT_SIZE = (
('50x50', '50x50'),
('50x70', '50x70'),
('70x50', '70x50'),
)
product_id = models.IntegerField()
product_size = models.CharField(max_length = 5, choices = PRODUCT_SIZE, default='50x50')
product_price = models.PositiveSmallIntegerField()
has_frame = models.BooleanField(default = False)
products = models.Manager()
products_with_frames = ProductWithFrames()
products_without_frames = ProductWithoutFrames()
def _has_frame(self):
if self.has_frame == True:
return True
return False
# _has_frame.boolean = True
# has_frame = property(_has_frame)
def __str__(self):
if self.has_frame:
return "Oprawiony rozmiar ramy {} kosztuje {} zl".format(self.product_size, self.product_price)
else:
return "Nieoprawiony rozmiar ramy {} kosztuje {} zl".format(self.product_size, self.product_price)
So in my own code I haven't got any String like "Jestem watowcem". I have just removed it for test but error is still visible.
python manage.py makemigration <app> goes correctly
python manage.py migrate - call errors like above
Only one file from Error List is editable and it is manage.py but I haven't edited it since beggining of project.
So if in my own code I haven't got String variable but Python want to tell me that it can't convert value, does anybody know what can I do or where should I remove this variable?

django.db.utils.OperationalError after removing migrations & makemigrations

I have deleted all migrations in the project, deleted the database and trying to re-create migrations with manage.py makemigrations command.
But I'm getting the error:
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/Users/rankor/src/python/web/Auction/Auction/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/management/base.py", line 398, in execute
self.check()
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/checks/urls.py", line 10, in check_url_config
return check_resolver(resolver)
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/checks/urls.py", line 19, in check_resolver
for pattern in resolver.url_patterns:
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/rankor/src/python/web/Auction/Auction/Auction/urls.py", line 14, in <module>
url(r'^orders/', include('orders.urls', namespace='orders')),
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/rankor/src/python/web/Auction/Auction/orders/urls.py", line 3, in <module>
from orders.views import CreateOrderView, OrdersView, OrderView
File "/Users/rankor/src/python/web/Auction/Auction/orders/views.py", line 7, in <module>
from orders.forms import OrderForm
File "/Users/rankor/src/python/web/Auction/Auction/orders/forms.py", line 47, in <module>
class OrderForm(ModelForm):
File "/Users/rankor/src/python/web/Auction/Auction/orders/forms.py", line 60, in OrderForm
widget=forms.Select, queryset=OrderCategory.objects.filter(parent__isnull=False).order_by('ordering'))
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/forms/models.py", line 1142, in __init__
self.queryset = queryset
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/forms/models.py", line 1168, in _set_queryset
self.widget.choices = self.choices
File "/Users/rankor/src/python/web/Auction/Auction/orders/forms.py", line 16, in _get_choices
if not self.queryset:
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/models/query.py", line 266, in __nonzero__
return type(self).__bool__(self)
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/models/query.py", line 262, in __bool__
self._fetch_all()
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__
results = compiler.execute_sql()
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 852, in execute_sql
cursor.execute(sql, params)
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 323, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: orders_ordercategory
OrderForm:
class OrderForm(ModelForm):
"""
Order form
"""
class Meta:
model = Order
fields = ('city', 'category', 'title', 'min_price', 'max_price', 'description', 'delivery_type', 'valid_to')
city = forms.ModelChoiceField(
widget=forms.Select, queryset=City.objects.filter(country__code='RU').order_by('name'))
category = CategoryChoiceField(
widget=forms.Select, queryset=OrderCategory.objects.filter(parent__isnull=False).order_by('ordering'))
valid_to = forms.DateField(widget=SelectDateWidget)
class GroupedModelChoiceField(forms.ModelChoiceField):
def optgroup_from_instance(self, obj):
return ""
def __choice_from_instance__(self, obj):
return obj.id, self.label_from_instance(obj)
def _get_choices(self):
if not self.queryset:
return []
all_choices = []
if self.empty_label:
current_optgroup = ""
current_optgroup_choices = [("", self.empty_label)]
else:
current_optgroup = self.optgroup_from_instance(self.queryset[0])
current_optgroup_choices = []
for item in self.queryset:
optgroup_from_instance = self.optgroup_from_instance(item)
if current_optgroup != optgroup_from_instance:
all_choices.append((current_optgroup, current_optgroup_choices))
current_optgroup_choices = []
current_optgroup = optgroup_from_instance
current_optgroup_choices.append(self.__choice_from_instance__(item))
all_choices.append((current_optgroup, current_optgroup_choices))
return all_choices
choices = property(_get_choices, forms.ChoiceField._set_choices)
class CategoryChoiceField(GroupedModelChoiceField):
def optgroup_from_instance(self, obj):
return obj.parent.title
Why and how to fix it?
P.S. Django 1.9, python 2.7, OS X.
Look at the stacktrace.
During the warm up process, Django imports all models and urls.
You should move queryset initialization to the init method of your OrderForm.
For a more general note, and to answer Lorenzo. In all cases you want to move fields querysets initialisation to the init method of your form or moved them to your views. Why? Actually, when Django is started, it can import some of your forms, the form class is built and the same happen with your field-class attributes. As a result, the queryset is fetched directly, once and for all, when importing your form and stays constant as long as you do not reload the server, so if you ever add data to your OrderCategory it will never appear in your select widget.
The "workaround" is to move queryset initialisation to the init method of the form.
In your case :
class OrderForm(ModelForm):
def __init__(self, *args, **kwargs):
super(OrderForm, self).__init__(*args, **kwargs)
self.fields['city'].queryset = City.objects.filter(country__code='RU').order_by('name')
self.fields['category'].queryset = OrderCategory.objects.filter(parent__isnull=False).order_by('ordering')
class Meta:
#...
# ... fields definition
See Fields which handle relationships for relative Django documentation.

django makemigrations Failing When Trying to Add ForeignKey to Existing Model

I have a model, and I'm trying to add a foreignkey field to it.
This is the field definition:
part_of_speech = models.ForeignKey('PartOfSpeech', help_text=_('Translation | part_of_speech | help_text'), verbose_name = _('Translation','part_of_speech'))
When I try to perform the makemigrations command, I get the following error:
Migrations for 'dictionary':
0024_translation_part_of_speech.py:
- Add field part_of_speech to translation
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/user/.virtualenvs/env2/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Users/user/.virtualenvs/env2/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/user/.virtualenvs/env2/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/user/.virtualenvs/env2/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/Users/user/.virtualenvs/env2/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 143, in handle
self.write_migration_files(changes)
File "/Users/user/.virtualenvs/env2/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 171, in write_migration_files
migration_string = writer.as_string()
File "/Users/user/.virtualenvs/env2/lib/python2.7/site-packages/django/db/migrations/writer.py", line 146, in as_string
operation_string, operation_imports = OperationWriter(operation).serialize()
File "/Users/user/.virtualenvs/env2/lib/python2.7/site-packages/django/db/migrations/writer.py", line 104, in serialize
_write(arg_name, arg_value)
File "/Users/user/.virtualenvs/env2/lib/python2.7/site-packages/django/db/migrations/writer.py", line 74, in _write
arg_string, arg_imports = MigrationWriter.serialize(_arg_value)
File "/Users/user/.virtualenvs/env2/lib/python2.7/site-packages/django/db/migrations/writer.py", line 354, in serialize
return cls.serialize_deconstructed(path, args, kwargs)
File "/Users/user/.virtualenvs/env2/lib/python2.7/site-packages/django/db/migrations/writer.py", line 248, in serialize_deconstructed
arg_string, arg_imports = cls.serialize(arg)
File "/Users/user/.virtualenvs/env2/lib/python2.7/site-packages/django/db/migrations/writer.py", line 276, in serialize
value = force_text(value)
File "/Users/user/.virtualenvs/env2/lib/python2.7/site-packages/django/utils/encoding.py", line 92, in force_text
s = six.text_type(s)
File "/Users/user/.virtualenvs/env2/lib/python2.7/site-packages/django/utils/functional.py", line 141, in __text_cast
return func(*self.__args, **self.__kw)
TypeError: ugettext() takes exactly 1 argument (2 given)
The help text declaration (the call to ugetttext) is formatted improperly. To solve this, change this:
verbose_name = _('Translation','part_of_speech')
to this:
verbose_name = _('Translation | part_of_speech')

Async query throws AssertionError first time (AppEngine, NDB)

When I use fetch_async() on a query it crashes with AssertionError the first time it is run. If I run it again immediately, it's fine.
Eg.
With model:
class User(ndb.Model):
user = ndb.UserProperty()
name = ndb.StringProperty()
penname = ndb.StringProperty()
first_login = ndb.DateTimeProperty(auto_now_add=True)
contact = ndb.BooleanProperty()
The following works straight away, returning an empty list:
users = User.query(User.penname == "asdf").fetch()
But this crashes:
future = User.query(User.penname == "asdf").fetch_async()
users = future.get_result()
With:
Traceback (most recent call last):
File "/opt/google-appengine-python/google/appengine/ext/admin/__init__.py", line 320, in post
exec(compiled_code, globals())
File "<string>", line 6, in <module>
File "/opt/google-appengine-python/google/appengine/ext/ndb/tasklets.py", line 320, in get_result
self.check_success()
File "/opt/google-appengine-python/google/appengine/ext/ndb/tasklets.py", line 357, in _help_tasklet_along
value = gen.throw(exc.__class__, exc, tb)
File "/opt/google-appengine-python/google/appengine/ext/ndb/query.py", line 887, in _run_to_list
batch = yield rpc
File "/opt/google-appengine-python/google/appengine/ext/ndb/tasklets.py", line 435, in _on_rpc_completion
result = rpc.get_result()
File "/opt/google-appengine-python/google/appengine/api/apiproxy_stub_map.py", line 592, in get_result
return self.__get_result_hook(self)
File "/opt/google-appengine-python/google/appengine/datastore/datastore_query.py", line 2386, in __query_result_hook
self._batch_shared.conn.check_rpc_success(rpc)
File "/opt/google-appengine-python/google/appengine/datastore/datastore_rpc.py", line 1191, in check_rpc_success
rpc.check_success()
File "/opt/google-appengine-python/google/appengine/api/apiproxy_stub_map.py", line 558, in check_success
self.__rpc.CheckSuccess()
File "/opt/google-appengine-python/google/appengine/api/apiproxy_rpc.py", line 156, in _WaitImpl
self.request, self.response)
File "/opt/google-appengine-python/google/appengine/api/datastore_file_stub.py", line 568, in MakeSyncCall
response)
File "/opt/google-appengine-python/google/appengine/api/apiproxy_stub.py", line 87, in MakeSyncCall
method(request, response)
File "/opt/google-appengine-python/google/appengine/datastore/datastore_stub_util.py", line 2367, in UpdateIndexesWrapper
self._UpdateIndexes()
File "/opt/google-appengine-python/google/appengine/datastore/datastore_stub_util.py", line 2656, in _UpdateIndexes
self._index_yaml_updater.UpdateIndexYaml()
File "/opt/google-appengine-python/google/appengine/datastore/datastore_stub_index.py", line 244, in UpdateIndexYaml
all_indexes, manual_indexes)
File "/opt/google-appengine-python/google/appengine/datastore/datastore_stub_index.py", line 85, in GenerateIndexFromHistory
required, kind, ancestor, props, num_eq_filters = datastore_index.CompositeIndexForQuery(query)
File "/opt/google-appengine-python/google/appengine/datastore/datastore_index.py", line 424, in CompositeIndexForQuery
assert filter.property(0).name() == ineq_property
AssertionError
But if I run it again immediately, for example by doing:
future = User.query(User.penname == "asdf").fetch_async()
try:
users = future.get_result()
except:
users = future.get_result()
It works.
This has been cropping up all over the place and I'm struggling to pin down the root cause.
The solution was to upgrade to the 1.7.1 SDK.

Categories

Resources