Related
I obtain the following error when typing mkdocs serve in terminal:
ERROR - Error reading page 'codereference.md': <class '_ast.ExtSlice'>
(with a very long traceback, see below)
My mkdocs.yaml file is:
site_name: Code Documentation
site_url: https://example.com/
nav:
- Home: index.md
- About: about.md
- Code: codereference.md
theme: readthedocs
plugins:
- mkdocstrings:
handlers:
python:
paths: [.]
The codereference.md file consists of the following:
# Reference
::: path.to.class.from.where.mkdocsyaml.is
Anybody have any possible answers?
The full traceback is:
Traceback (most recent call last):
File "/usr/local/bin/mkdocs", line 8, in <module>
sys.exit(cli())
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1130, in __call__
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1055, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/mkdocs/__main__.py", line 181, in serve_command
serve.serve(dev_addr=dev_addr, livereload=livereload, watch=watch, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/mkdocs/commands/serve.py", line 63, in serve
config = builder()
File "/usr/local/lib/python3.8/dist-packages/mkdocs/commands/serve.py", line 58, in builder
build(config, live_server=live_server, dirty=dirty)
File "/usr/local/lib/python3.8/dist-packages/mkdocs/commands/build.py", line 292, in build
_populate_page(file.page, config, files, dirty)
File "/usr/local/lib/python3.8/dist-packages/mkdocs/commands/build.py", line 174, in _populate_page
page.render(config, files)
File "/usr/local/lib/python3.8/dist-packages/mkdocs/structure/pages.py", line 175, in render
self.content = md.convert(self.markdown)
File "/usr/local/lib/python3.8/dist-packages/markdown/core.py", line 264, in convert
root = self.parser.parseDocument(self.lines).getroot()
File "/usr/local/lib/python3.8/dist-packages/markdown/blockparser.py", line 90, in parseDocument
self.parseChunk(self.root, '\n'.join(lines))
File "/usr/local/lib/python3.8/dist-packages/markdown/blockparser.py", line 105, in parseChunk
self.parseBlocks(parent, text.split('\n\n'))
File "/usr/local/lib/python3.8/dist-packages/markdown/blockparser.py", line 123, in parseBlocks
if processor.run(parent, blocks) is not False:
File "/usr/local/lib/python3.8/dist-packages/mkdocstrings/extension.py", line 121, in run
html, handler, data = self._process_block(identifier, block, heading_level)
File "/usr/local/lib/python3.8/dist-packages/mkdocstrings/extension.py", line 195, in _process_block
data: CollectorItem = handler.collect(identifier, options)
File "/usr/local/lib/python3.8/dist-packages/mkdocstrings_handlers/python/handler.py", line 191, in collect
loader.load_module(module_name)
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 148, in load_module
top_module = self._load_module(package.name, package.path, submodules=submodules)
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 352, in _load_module
return self._load_module_path(module_name, module_path, submodules, parent)
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 380, in _load_module_path
self._load_submodules(module)
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 385, in _load_submodules
self._load_submodule(module, subparts, subpath)
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 396, in _load_submodule
member_parent[subparts[-1]] = self._load_module(
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 352, in _load_module
return self._load_module_path(module_name, module_path, submodules, parent)
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 374, in _load_module_path
module = self._visit_module(code, module_name, module_path, parent)
File "/usr/local/lib/python3.8/dist-packages/griffe/loader.py", line 413, in _visit_module
module = visit(
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 92, in visit
return Visitor(
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 172, in get_module
self.visit(top_node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 183, in visit
super().visit(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/base.py", line 19, in visit
getattr(self, f"visit_{node.kind}", self.generic_visit)(node) # type: ignore[attr-defined]
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 214, in visit_module
self.generic_visit(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 196, in generic_visit
self.visit(child)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 183, in visit
super().visit(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/base.py", line 19, in visit
getattr(self, f"visit_{node.kind}", self.generic_visit)(node) # type: ignore[attr-defined]
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 255, in visit_classdef
self.generic_visit(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 196, in generic_visit
self.visit(child)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 183, in visit
super().visit(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/base.py", line 19, in visit
getattr(self, f"visit_{node.kind}", self.generic_visit)(node) # type: ignore[attr-defined]
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 451, in visit_functiondef
self.handle_function(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 442, in handle_function
self.generic_visit(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 196, in generic_visit
self.visit(child)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 183, in visit
super().visit(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/base.py", line 19, in visit
getattr(self, f"visit_{node.kind}", self.generic_visit)(node) # type: ignore[attr-defined]
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 589, in visit_assign
self.handle_attribute(node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/visitor.py", line 548, in handle_attribute
value = get_value(node.value) # type: ignore[arg-type]
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/nodes.py", line 1181, in get_value
return _node_value_map[type(node)](node)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/nodes.py", line 1047, in _get_subscript_value
subscript = _get_value(node.slice)
File "/usr/local/lib/python3.8/dist-packages/griffe/agents/nodes.py", line 1167, in _get_value
return _node_value_map[type(node)](node)
KeyError: <class '_ast.ExtSlice'>
It's fixed in version 0.21.0 of Griffe: https://mkdocstrings.github.io/griffe/changelog/#bug-fixes
It would previously crash when unparsing values using extended slices such as o[x:y,z].
I am trying to run a migration to add a foreign key constraint. This is when I try to add a artist_id to my RSVP model on a many to one relationship. Each RSVP can only have an artist, and an artist can have many rsvps.
Here is my migration code
def upgrade():
with op.batch_alter_table('rsvp') as batch_op:
batch_op.create_foreign_key(
"fk_artist_rsvp", "rsvp",
"artist", ["artist_id"], ["id"])
def downgrade():
with op.batch_alter_table('rsvp') as batch_op:
batch_op.drop_constraint(
u'fk_artist_rsvp', 'artist', type_='foreignkey')
When I run it I get the following error
Traceback (most recent call last):
File "/Users/dariusmandres/.pyenv/versions/st-art/bin/alembic", line 11, in <module>
load_entry_point('alembic==1.4.0', 'console_scripts', 'alembic')()
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/alembic/config.py", line 577, in main
CommandLine(prog=prog).main(argv=argv)
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/alembic/config.py", line 571, in main
self.run_cmd(cfg, options)
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/alembic/config.py", line 551, in run_cmd
**dict((k, getattr(options, k, None)) for k in kwarg)
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/alembic/command.py", line 298, in upgrade
script.run_env()
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/alembic/script/base.py", line 489, in run_env
util.load_python_file(self.dir, "env.py")
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/alembic/util/pyfiles.py", line 98, in load_python_file
module = load_module_py(module_id, path)
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/alembic/util/compat.py", line 173, in load_module_py
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "alembic/env.py", line 77, in <module>
run_migrations_online()
File "alembic/env.py", line 71, in run_migrations_online
context.run_migrations()
File "<string>", line 8, in run_migrations
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/alembic/runtime/environment.py", line 846, in run_migrations
self.get_context().run_migrations(**kw)
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/alembic/runtime/migration.py", line 518, in run_migrations
step.migration_fn(**kw)
File "/Users/dariusmandres/projects/st-art/alembic/versions/1d80258c8056_artist_rsvp.py", line 23, in upgrade
"artist", ["artist_id"], ["id"])
File "<string>", line 3, in create_foreign_key
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/alembic/operations/ops.py", line 702, in batch_create_foreign_key
return operations.invoke(op)
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/alembic/operations/base.py", line 374, in invoke
return fn(self, operation)
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/alembic/operations/toimpl.py", line 151, in create_constraint
operation.to_constraint(operations.migration_context)
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/alembic/operations/ops.py", line 547, in to_constraint
**self.kw
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/alembic/operations/schemaobj.py", line 46, in foreign_key_constraint
schema=referent_schema
File "<string>", line 2, in __new__
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/sqlalchemy/util/deprecations.py", line 130, in warned
return fn(*args, **kwargs)
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/sqlalchemy/sql/schema.py", line 470, in __new__
key = _get_table_key(name, schema)
File "/Users/dariusmandres/.pyenv/versions/3.7.0/envs/st-art/lib/python3.7/site-packages/sqlalchemy/sql/schema.py", line 77, in _get_table_key
return schema + "." + name
TypeError: can only concatenate list (not "str") to list
What is going on?
The problem stems from the different syntax between op.create_foreign_key and batch_op.create_foreign_key…
def upgrade():
with op.batch_alter_table('rsvp') as batch_op:
batch_op.create_foreign_key(
"fk_artist_rsvp", "rsvp",
"artist", ["artist_id"], ["id"])
Should be:
def upgrade():
with op.batch_alter_table('rsvp') as batch_op:
batch_op.create_foreign_key(
"fk_artist_rsvp",
"artist", ["artist_id"], ["id"])
(no need to provide the table name inside a batch op)
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?
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()
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.