django2 migrations ProgrammingError [duplicate] - django

I am getting an error:
$ python manage.py migrate swsite 0023_hitcounter.py
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib64/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/lib64/python2.7/site-packages/django/core/management/base.py", line 398, in execute
self.check()
File "/usr/lib64/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/usr/lib64/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/lib64/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/usr/lib64/python2.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "/usr/lib64/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/lib64/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/lib64/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/lib64/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/var/www/swlab-website/mysite/urls.py", line 25, in <module>
url(r'^swsite/', include('swsite.urls')),
File "/usr/lib64/python2.7/site-packages/django/conf/urls/__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/var/www/swlab-website/swsite/urls.py", line 2, in <module>
from . import views
File "/var/www/swlab-website/swsite/views.py", line 27, in <module>
class IndexView(generic.ListView):
File "/var/www/swlab-website/swsite/views.py", line 31, in IndexView
newhit = HitCounter.objects.create()
File "/usr/lib64/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/django/db/models/query.py", line 401, in create
obj.save(force_insert=True, using=self.db)
File "/usr/lib64/python2.7/site-packages/django/db/models/base.py", line 708, in save
force_update=force_update, update_fields=update_fields)
File "/usr/lib64/python2.7/site-packages/django/db/models/base.py", line 736, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/lib64/python2.7/site-packages/django/db/models/base.py", line 820, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/lib64/python2.7/site-packages/django/db/models/base.py", line 859, in _do_insert
using=using, raw=raw)
File "/usr/lib64/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/django/db/models/query.py", line 1039, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/lib64/python2.7/site-packages/django/db/models/sql/compiler.py", line 1060, in execute_sql
cursor.execute(sql, params)
File "/usr/lib64/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/lib64/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/lib64/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/lib64/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "swsite_hitcounter" does not exist
LINE 1: INSERT INTO "swsite_hitcounter" ("date", "template_location"...
This is confusing to me as it is specifically trying to build the swsite_hitcounter table as in the following migration I am trying to fun:
# -*- coding: utf-8 -*-
# Generated by Django 1.9.5 on 2017-07-05 15:56
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('swsite', '0022_auto_20170307_1343'),
]
operations = [
migrations.CreateModel(
name='HitCounter',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date', models.DateTimeField(auto_now=True)),
('template_location', models.TextField()),
],
),
]
Is something maybe out of sink? This is me trying to run this migration on my production box, it ran fine (of course) on my development box. Might of been cause I ran a specific migration? :
python manage.py migrate swsite 0023_hitcounter.py
Though running:
python manage.py showmigrations
Gives me same error? I am guessing my migrations are out of sync but not being able to show migrations, I don't know how to show the ones that didn't get into this update (on production from gilab)

The traceback is showing you that the error is occuring in IndexView. You are trying to create objects in the database when the view loads.
class IndexView(TemplateView):
newhit = HitCounter.objects.create() # remove this line
...
Accessing the database when the views load like this is a bad idea, so you should probably remove the line. In production, it gives you the error because it is trying to create the object in the database before you have applied the migration that creates the table.

Related

Where to put Django rest framework code in a Django project?

I'd like to know where/in which file I should put a set of DRF python code in a Django project. Here's why/what happening to me now.
As I'm trying to migrate from the default sqlite database to postgres, I have a trouble;
$ IS_PRODUCTION=true python manage.py showmigrations
..
django.db.utils.ProgrammingError: relation "django_content_type" does not exist
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...
that error tells me relation "django_content_type" does not exist, so I checked the stack trace in the error;
..
File "/root/pg/django/snippetapp/app/urls.py", line 623, in PostViewSet
_content_type_id = ContentType.objects.get_for_model(Post).id
..
It seems that I got the error because I have the set of DRF scripts on ./appname/urls.py,
and I imported/used ContentType, one of core Django classes in the file;
since it seemingly Django loads the top urls.py file in a very early phase, over dealing with ContentType stuff.
So, I guess I got to move the whole set of DRF, so where should I?
But one thing, I guess we have urlpatterns include-ing DRF router urls as such:
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'posts', PostViewSet)
..
urlpatterns = [
path('api/', include(router.urls)),
..
Then isn't we also need to have DRF scripts "before" the ./appname/urls.py -> urlpatterns...?
So I'd also like to know, when moving them to an another file cannot be an option, how can I solve/workaround the issue I have on importing ContentType in the urls.py?
Thanks.
Edit as requested
Here's the the PostViewSet that contains the imported ContentType in it;
from django.contrib.contenttypes.models import ContentType
..
class PostViewSet(viewsets.ModelViewSet):
"""
default Post viewset
"""
_site_id = settings.SITE_ID
# e.g. 3
_content_type_id = ContentType.objects.get_for_model(Post).id
# e.g. 12
comment_count = Comment.objects.filter(content_type_id=_content_type_id).filter(site_id=_site_id).filter(
object_pk=OuterRef('pk')).order_by().annotate(
count=Func(F('id'), function='COUNT')).values("count")
comments = Comment.objects.filter(content_type_id=_content_type_id).filter(site_id=_site_id).filter(
object_pk=OuterRef('pk'))
queryset = Post.objects.all().annotate(
vote_count=Count('voters'),
comment_count=Count('comments'),
# gets the comment count
comment02_count=Subquery(comment_count),
# gets the last comment
comment_last=Subquery(comments.order_by("-submit_date").values("comment")[:1]),
).all()
serializer_class = PostSerializer
permission_classes = [GeneralUserObjectPerm,]
filter_backends = [filters.OrderingFilter, DynamicSearchFilter,]
pagination_class = StandardResultsSetPagination
The entire stack track in the error
# IS_PRODUCTION=true python manage.py showmigrations
/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
Traceback (most recent call last):
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "django_content_type" does not exist
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/core/management/base.py", line 368, in execute
self.check()
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/core/management/base.py", line 396, in check
databases=databases,
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/core/checks/registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/urls/resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/urls/resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/urls/resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "/root/.pyenv/versions/3.7.6/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/root/pg/django/snippetapp/app/urls.py", line 615, in <module>
class PostViewSet(viewsets.ModelViewSet):
File "/root/pg/django/snippetapp/app/urls.py", line 623, in PostViewSet
_content_type_id = ContentType.objects.get_for_model(Post).id
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/contrib/contenttypes/models.py", line 51, in get_for_model
ct = self.get(app_label=opts.app_label, model=opts.model_name)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/db/models/query.py", line 425, in get
num = len(clone)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/db/models/query.py", line 269, in __len__
self._fetch_all()
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/db/models/query.py", line 1308, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1156, in execute_sql
cursor.execute(sql, params)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/root/pg/django/snippetapp/venv_linux_v2/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "django_content_type" does not exist
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...
#

Initial local Django mysql database

I have joined a team working on a Django project, and I am trying to setup the local mysql database schema. This project has existed for a long time, so there are existing migrations.
I have created the empty database in mysql, but when I try to apply migrations to create the schema, it complains that the tables don't exist yet.
$ ./manage.py migrate
Traceback (most recent call last):
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
res = self._query(query)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 374, in _do_query
db.query(q)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/connections.py", line 277, in query
_mysql.connection.query(self, query)
_mysql_exceptions.ProgrammingError: (1146, "Table 'api_test.headset_product' doesn't exist")
I've also tried syncdb, migrate --run-syncdb, and makemigrations with the same results. I've also tried loaddata with our fixtures, with the same results.
I've read Django : Table doesn't exist, but I didn't drop tables in this case (no tables have ever existed), and I don't want to recreate all the migrations (I'm not the only person on this project).
I have a related project that uses a local postgresql database, and it doesn't seem to have this problem. I can run migrate and it does just what I expect, so it feels that I've run into a problem specific to this codebase, but we aren't certain how to debug it.
"full output of makemigrations, including the full typed command."
$ ./manage.py makemigrations
[WARNING] statsd not configured
[WARNING] SMTP not configured; emails will not be sent
[WARNING] V1 database access not configured
Traceback (most recent call last):
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
res = self._query(query)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 374, in _do_query
db.query(q)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/connections.py", line 277, in query
_mysql.connection.query(self, query)
_mysql_exceptions.ProgrammingError: (1146, "Table 'reign_api_test.headset_product' doesn't exist")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "./manage.py", line 14, in <module>
execute_from_command_line(sys.argv)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/core/management/base.py", line 332, in execute
self.check()
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/core/management/base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/core/management/base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/core/checks/registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/urls/resolvers.py", line 397, in check
for pattern in self.url_patterns:
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/urls/resolvers.py", line 536, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/urls/resolvers.py", line 529, in urlconf_module
return import_module(self.urlconf_name)
File "/Users/rnapier/.pyenv/versions/3.6.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/rnapier/clients/Jaybird/Source/reign-api/reign_api/reign_api/urls.py", line 13, in <module>
import newsfeed.urls
File "/Users/rnapier/clients/Jaybird/Source/reign-api/reign_api/newsfeed/urls.py", line 2, in <module>
from newsfeed import views
File "/Users/rnapier/clients/Jaybird/Source/reign-api/reign_api/newsfeed/views.py", line 15, in <module>
from newsfeed import models, serializers
File "/Users/rnapier/clients/Jaybird/Source/reign-api/reign_api/newsfeed/serializers.py", line 27, in <module>
class NewsfeedSubscriptionSerializer(serializers.Serializer):
File "/Users/rnapier/clients/Jaybird/Source/reign-api/reign_api/newsfeed/serializers.py", line 28, in NewsfeedSubscriptionSerializer
PRODUCTS = [f for f in headset.models.Product.objects.values_list("name", flat=True)]
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/models/query.py", line 272, in __iter__
self._fetch_all()
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/models/query.py", line 1179, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/models/query.py", line 178, in __iter__
for row in compiler.results_iter(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size):
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1017, in results_iter
results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1066, in execute_sql
cursor.execute(sql, params)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
res = self._query(query)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 374, in _do_query
db.query(q)
File "/Users/rnapier/.pyenv/versions/api-3.6.6/lib/python3.6/site-packages/MySQLdb/connections.py", line 277, in query
_mysql.connection.query(self, query)
django.db.utils.ProgrammingError: (1146, "Table 'reign_api_test.headset_product' doesn't exist")
"model structure for headset_product"
class Product(models.Model):
"""
products supported by the Headset API
"""
name = models.CharField(max_length=128, unique=True)
def __str__(self) -> str:
return "<Product {s.id} {s.name}>".format(s=self)
__repr__ = __str__
The problem is in reign_api/newsfeed/serializers.py, which is trying to make a query at class or module level. This should always be avoided in Django, as the query will be run at first import - before the migrations on a new system will have a chance to run. But this will be causing other problems too, in that that query will only be executed once per process, and will therefore not update when new data is added or changed.
Your colleagues should fix this problem, either by refactoring to move the query into a method, or probably better by using a more appropriate serializer field that takes a queryset and does the query when called.

Django DB migration issue: non-existent attribute

After removing an attribute from a model (sqlite3):
class Course(models.Model):
Name = models.CharField(max_length=30,unique=True)
Active = models.BooleanField(default=True)
# rubric = models.ForeignKey(Rubric) #removed attribute
def __unicode__(self):
return u'%s' % (self.Name)
I get errors upon using migrate (makemigrations is fine):
django.db.utils.IntegrityError: NOT NULL constraint failed: gbook_course.rubric_id
This field doesn't exist anymore! I don't understand where this is failing, or where to fix it. Re-adding the attribute and using null=True, blank=True and changing the attribute to a CharField or other type also do not fix the problem.
Opening the DB from the sqlite command-line application and looking at the schema for the course table, I get the indication that there's no rubric field there:
CREATE TABLE "gbook_course" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "Active" bool NOT NULL, "Name" varchar(30) NOT NULL UNIQUE);
On a related note, I figured that maybe I had just borked the DB, so I also tried deleting it and the app's migrations (except for __init__.py), which should start the thing over from scratch, but I end up with an error both from the runserver and from the migrations:
django.db.utils.OperationalError: no such table: gbook_student
At this point, there's no DB in existence (totally deleted), so I'm really unsure as to where this error could possibly come from. A solution to either problem is fine - I can re-start with a blank DB, if needed.
migrate traceback:
ltjgates:gbsite jgates$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, gbook, sessions
Running migrations:
Rendering model states... DONE
Applying gbook.0011_course_rubric...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 356, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 202, in handle
targets, plan, fake=fake, fake_initial=fake_initial
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/executor.py", line 97, in migrate
state = self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/executor.py", line 132, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/executor.py", line 237, in apply_migration
state = migration.apply(state, schema_editor)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards
field,
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 231, in add_field
self._remake_table(model, create_fields=[field])
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 199, in _remake_table
self.quote_name(model._meta.db_table),
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 112, in execute
cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: gbook_course.rubric_id
Deleted DB traceback (makemigrations):
ltjgates:gbsite jgates$ python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 353, in execute
self.check()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 385, in check
include_deployment_checks=include_deployment_checks,
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 372, in _run_checks
return checks.run_checks(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/urls/resolvers.py", line 310, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/urls/resolvers.py", line 303, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Volumes/ProfileHD/jgates/Desktop/Gbook/gbsite/gbsite/urls.py", line 21, in <module>
url(r'^gbook/', include('gbook.urls')),
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Volumes/ProfileHD/jgates/Desktop/Gbook/gbsite/gbook/urls.py", line 2, in <module>
from . import views
File "/Volumes/ProfileHD/jgates/Desktop/Gbook/gbsite/gbook/views.py", line 12, in <module>
from gbook.forms import *
File "/Volumes/ProfileHD/jgates/Desktop/Gbook/gbsite/gbook/forms.py", line 38, in <module>
class StudentChooseForm(forms.ModelForm):
File "/Volumes/ProfileHD/jgates/Desktop/Gbook/gbsite/gbook/forms.py", line 39, in StudentChooseForm
class Meta:
File "/Volumes/ProfileHD/jgates/Desktop/Gbook/gbsite/gbook/forms.py", line 45, in Meta
'Email': CheckboxSelectMultiple(choices=( (x.id, x.FirstName+" "+x.LastName) for x in CHOICES )),
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 256, in __iter__
self._fetch_all()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 1085, in _fetch_all
self._result_cache = list(self.iterator())
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: gbook_student
Still not sure of the exact cause, but this worked (in the SQLite command line tool):
sqlite> select * from gbook_course;
1|1|Honors Physics
2|1|Physics
3|1|AP Physics
sqlite> delete from gbook_course;
Now the schema shows up with:
CREATE TABLE "gbook_course" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "Name" varchar(30) NOT NULL UNIQUE, "Active" bool NOT NULL, "rubric_id" integer NULL REFERENCES "gbook_rubric" ("id"));
CREATE INDEX "gbook_course_8980b7ae" ON "gbook_course" ("rubric_id");

Why do i have 'relation does not exist' when just running 'makemigrations'?

Django-based project made use of Django 1.6 and now needs upgrade to 1.9. So i just renamed South's 'migrations' folders to 'south_migrations', created 'migrations' folder with init.py in each app that is in INSTALLED_APPS, upgraded all requirements of a project and made changes to code so it would be Django1.9-compatible. Now i want to create native Django migrations running 'makemigrations' but getting this traceback:
Traceback (most recent call last):
File "/home/nervosa/Apps/pycharm-2016.1/helpers/pycharm/django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)
File "/usr/lib/python2.7/runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/nervosa/DjangoProjects/iticket/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 398, in execute
self.check()
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/nervosa/DjangoProjects/iticket/iticket/urls.py", line 7, in <module>
from frontend.views import ReturnPaymentURL
File "/home/nervosa/DjangoProjects/iticket/frontend/views.py", line 19, in <module>
from cases.forms import SelfTrafficForm, SelfStartTrafficForm, PJCForm
File "/home/nervosa/DjangoProjects/iticket/cases/forms.py", line 269, in <module>
class SelfTrafficForm(forms.ModelForm):
File "/home/nervosa/DjangoProjects/iticket/cases/forms.py", line 271, in SelfTrafficForm
counties = [(x.pk, x.county.title()) for x in County.objects.all()]
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
self._fetch_all()
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__
results = compiler.execute_sql()
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "places_county" does not exist
LINE 1: ...."main_attorney_id", "places_county"."ds_id" FROM "places_co...
^
When running for separate app, e.g. 'makemigrations billing' i get just the same output. I'm kinda puzzled because even Django's tables aren't created!!!
What is the best way to overcome this?
Your TrafficForm is doing something it shouldn't, which is querying the County objects at import time. While you had an existing database, this was OK(*), but now that you're starting from scratch it will complain.
You should change that form to use ModelChoiceField.
(*) it's almost always a bad idea to do this anyway, because new objects won't be visible in that field until the process is restarted; but in your case I guess new County objects are not often created.

ProgrammingError when upgrading from Django 1.8 to 1.9

I have project with multiple apps in it. One of the apps has a Contact model and a Supplier model. The Supplier has a OneToOne relation to the Contact model. Everything worked fine in Django 1.8.11. When I attempt to makemigrations or any other command, I get the following error.
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "D:\env\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
utility.execute()
File "D:\env\lib\site-packages\django\core\management\__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\env\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\env\lib\site-packages\django\core\management\base.py", line 398, in execute
self.check()
File "D:\env\lib\site-packages\django\core\management\base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "D:\env\lib\site-packages\django\core\checks\registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "D:\env\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "D:\env\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "D:\env\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\env\lib\site-packages\django\core\urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "D:\env\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\env\lib\site-packages\django\core\urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "D:\mdjango\mdjango\urls.py", line 32, in <module>
url(r'^parts?/', include('parts.urls')),
File "D:\env\lib\site-packages\django\conf\urls\__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "D:\mdjango\parts\urls.py", line 2, in <module>
from parts import views, ajax_views
File "D:\mdjango\parts\views.py", line 9, in <module>
from parts import forms
File "D:\mdjango\parts\forms.py", line 17, in <module>
class PartForm(forms.ModelForm):
File "D:\mdjango\parts\forms.py", line 20, in PartForm
class Meta:
File "D:\mdjango\parts\forms.py", line 26, in Meta
"/contacts/create/?next=/parts/create", "Add Supplier")
File "D:\mdjango\general\widgets.py", line 9, in __init__
super(SelectWithButton, self).__init__(attrs, choices)
File "D:\env\lib\site-packages\django\forms\widgets.py", line 514, in __init__
self.choices = list(choices)
File "D:\env\lib\site-packages\django\db\models\query.py", line 258, in __iter__
self._fetch_all()
File "D:\env\lib\site-packages\django\db\models\query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "D:\env\lib\site-packages\django\db\models\query.py", line 52, in __iter__
results = compiler.execute_sql()
File "D:\env\lib\site-packages\django\db\models\sql\compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "D:\env\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "D:\env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "D:\env\lib\site-packages\django\db\utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "D:\env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "contacts_supplier" does not exist
LINE 1: ...tacts_supplier"."default_contract_target_id" FROM "contacts_...
Basically, I have a URL file in my Parts app that is importing from parts.views, which is standard Django procedure. The parts.views file is importing from parts.forms, also standard Django procedure. The form that is causing issues is this:
class PartForm(forms.ModelForm):
class Meta:
model = Part
fields = ['supplier', 'number', 'name']
widgets = {
'supplier': SelectWithButton(None, Supplier.objects.all(),
"/contacts/create/?next=/parts/create", "Add Supplier")
}
Here's the SelectWithButton class:
from django.forms import Select
from django.template.loader import get_template
class SelectWithButton(Select):
def __init__(self, attrs=None, choices=(), btn_url='', btn_txt=''):
super(SelectWithButton, self).__init__(attrs, choices)
self.btn_url = btn_url
self.btn_txt = btn_txt
def render(self, name, value, attrs=None, choices=()):
context = {'href': self.btn_url, 'text': self.btn_txt,
'select_field': super(SelectWithButton, self).render(name, value, attrs, choices)}
t = get_template("general\selectWithButton.html")
return t.render(context)
The error seems to be that it is trying to get all the objects from Supplier before the migrations have been done. But I'm trying to make the migrations, and I'm getting this error. I did read that Django 1.9 now checks the URL files in the manage.py command, but it seems that I have done nothing out of the ordinary. You can read more here on the 1.9.3 page: https://docs.djangoproject.com/en/1.9/releases/1.9.3/
When I comment out the reference to SelectWithButton, I get a similar error on another form that has an autocomplete_light.MultipleChoiceField on it. What am I doing wrong? It works fine in Django 1.8.11.
Here is the other error and form:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "D:\env\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
utility.execute()
File "D:\env\lib\site-packages\django\core\management\__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\env\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\env\lib\site-packages\django\core\management\base.py", line 398, in execute
self.check()
File "D:\env\lib\site-packages\django\core\management\base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "D:\env\lib\site-packages\django\core\checks\registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "D:\env\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "D:\env\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "D:\env\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\env\lib\site-packages\django\core\urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "D:\env\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\env\lib\site-packages\django\core\urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "D:\mdjango\mdjango\urls.py", line 33, in <module>
url(r'^transactions?/', include('transactions.urls')),
File "D:\env\lib\site-packages\django\conf\urls\__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "D:\mdjango\transactions\urls.py", line 3, in <module>
from views import *
File "D:\mdjango\transactions\views.py", line 3, in <module>
from forms import *
File "D:\mdjango\transactions\forms.py", line 18, in <module>
class InventoryTransactionForm(forms.ModelForm):
File "D:\mdjango\transactions\forms.py", line 32, in InventoryTransactionForm
required=False)
File "D:\env\lib\site-packages\autocomplete_light\fields.py", line 74, in __init__
self.get_choices(autocomplete, registry, widget)})
File "D:\env\lib\site-packages\autocomplete_light\fields.py", line 82, in get_choices
return ((a.choice_value(c), a.choice_label(c)) for c in a.choices)
File "D:\env\lib\site-packages\django\db\models\query.py", line 258, in __iter__
self._fetch_all()
File "D:\env\lib\site-packages\django\db\models\query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "D:\env\lib\site-packages\django\db\models\query.py", line 52, in __iter__
results = compiler.execute_sql()
File "D:\env\lib\site-packages\django\db\models\sql\compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "D:\env\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "D:\env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "D:\env\lib\site-packages\django\db\utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "D:\env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "parts_sns" does not exist
LINE 1: SELECT "parts_sns"."id", "parts_sns"."sn" FROM "parts_sns"
The form:
class InventoryTransactionForm(forms.ModelForm):
parent_sn = autocomplete_light.ModelChoiceField(label="Parent SN", autocomplete='InventoryParentSNAutocomplete',
required=False)
qty_in = forms.FloatField(label="In", required=False)
qty_out = forms.FloatField(label="Out", required=False)
notes = forms.CharField(required=False)
part = autocomplete_light.ModelChoiceField(autocomplete="StandardAutocomplete")
document = autocomplete_light.ModelChoiceField(autocomplete="OpenDocumentAutocomplete", required=False)
# for Qty Out serial numbers
sns = autocomplete_light.MultipleChoiceField(label="SNs", autocomplete='InventoryQtyOutSNsAutocomplete',
required=False)
user = forms.ModelChoiceField(Employee.objects.filter(user__is_active=True))
class Meta:
model = Transaction
fields = ['date', 'usage', 'notes', 'user', 'sns']
Your form is causing the Supplier.objects.all() queryset to be evaluated before the supplier table has been created in the database.
You can get around this by setting the widget in the form's __init__ method.
class PartForm(forms.ModelForm):
class Meta:
model = Part
fields = ['supplier', 'number', 'name']
def __init__(self, *args, **kwargs):
super(PartForm, self).__init__(*args, **kwargs)
self.fields['supplier'].widget = SelectWithButton(
None,
Supplier.objects.all(),
"/contacts/create/?next=/parts/create",
"Add Supplier",
)