Error while fetching objects from Django model - django

I want to get all Codes Model objects and count the length of the objects returned. I'm getting an error that I can't understand.
This is my model
class Codes(models.Model):
username = models.CharField(max_length=100)
code = models.CharField(max_length=10000)
language = models.CharField(max_length=100)
def __str__(self):
return self.language
I'm using the below function to get all objects from the model
def get_all_codes(lang):
queryset = Codes.objects.filter(language = lang)
return queryset
Calling the function
all_codes_of_lang = get_all_codes(lang)
and trying to count the length using:
if len(all_codes_of_lang) > 0:
print("abc")
leading to this error:
if len(all_codes_of_lang) > 0:
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 240, in __len__
self._fetch_all()
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__
results = compiler.execute_sql()
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
ProgrammingError: relation "submissions_codes" does not exist
LINE 1: ...odes"."code", "submissions_codes"."language" FROM "submissio...
^
-->

This problem is not with the get_all_codes function or with counting the length, but with a synchronization problem between your models and your database.
Run python manage.py makemigrations and python manage.py migrate to fix.

The models doesn't migrated that's the reason you got this error.
Run following commands
python manage.py makemigrations
python manage.py migrate

Related

Heroku deployment of django project causing: Exception Type: ProgrammingError [duplicate]

This question already has answers here:
Django: ProgrammingError relation does not exists
(2 answers)
Relation does not exist - Django & Postgres
(3 answers)
Django on Heroku: relation does not exist
(5 answers)
Closed last month.
I have tested this locally and it works fine. When I deploy to Heroku it deploys fine and serves the home page. Though When I try to sign up and create a new user I get a Internal 500 error and get the following error message.
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
The above exception (relation "accounts_customuser" does not exist
LINE 1: SELECT 1 AS "a" FROM "accounts_customuser" WHERE "accounts_c...
^
) was the direct cause of the following exception:
File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/app/.heroku/python/lib/python3.10/site-packages/django/views/generic/base.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
File "/app/.heroku/python/lib/python3.10/site-packages/django/views/generic/base.py", line 142, in dispatch
return handler(request, *args, **kwargs)
File "/app/.heroku/python/lib/python3.10/site-packages/django/views/generic/edit.py", line 184, in post
return super().post(request, *args, **kwargs)
File "/app/.heroku/python/lib/python3.10/site-packages/django/views/generic/edit.py", line 152, in post
if form.is_valid():
File "/app/.heroku/python/lib/python3.10/site-packages/django/forms/forms.py", line 205, in is_valid
return self.is_bound and not self.errors
File "/app/.heroku/python/lib/python3.10/site-packages/django/forms/forms.py", line 200, in errors
self.full_clean()
File "/app/.heroku/python/lib/python3.10/site-packages/django/forms/forms.py", line 439, in full_clean
self._post_clean()
File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/auth/forms.py", line 129, in _post_clean
super()._post_clean()
File "/app/.heroku/python/lib/python3.10/site-packages/django/forms/models.py", line 498, in _post_clean
self.validate_unique()
File "/app/.heroku/python/lib/python3.10/site-packages/django/forms/models.py", line 507, in validate_unique
self.instance.validate_unique(exclude=exclude)
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/base.py", line 1226, in validate_unique
errors = self._perform_unique_checks(unique_checks)
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/base.py", line 1336, in _perform_unique_checks
if qs.exists():
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/query.py", line 1225, in exists
return self.query.has_results(using=self.db)
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/sql/query.py", line 592, in has_results
return compiler.has_results()
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1363, in has_results
return bool(self.execute_sql(SINGLE))
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1395, in execute_sql
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 103, in execute
return super().execute(sql, params)
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
with self.db.wrap_database_errors:
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
Exception Type: ProgrammingError at /accounts/signup/
Exception Value: relation "accounts_customuser" does not exist
LINE 1: SELECT 1 AS "a" FROM "accounts_customuser" WHERE "accounts_c...
Custom User Model
class CustomUser(AbstractUser):
age = models.PositiveBigIntegerField(null=True, blank=True)
business_name = models.CharField(null=True, blank=True, max_length=500)
first_name = models.CharField(null=True, blank=True, max_length=500)
last_name = models.CharField(null=True, blank=True, max_length=500)
Settings.py
AUTH_USER_MODEL = 'accounts.CustomUser'
Locally I have been testing using sqllite though I am using postgresql in heroku. I have set environment variables and it seems to be working upon deployment.
env = Env()
env.read_env()
...
DATABASES = {
'default': env.dj_db_url("DATABASE_URL")
}
Did you remember to make migrations and migrate?
heroku run python manage.py makemigrations and
heroku run python manage.py migrate

ProgrammingError at column "" does not exist

I added a custom extension to djangos User model and now I'm getting this error on my localhost url:
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column users_account.birthday does not exist
LINE 1: ... "users_account"."id", "users_account"."user_id", "users_acc...
^
Furthermore, when I tried to migrate my changes I got this error in the terminal:
Operations to perform:
Apply all migrations: admin, auth, chaburah, contenttypes, sessions, taggit, users
Running migrations:
Applying users.0002_alter_account_birthday...Traceback (most recent call last):
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedColumn: column "birthday" does not exist
LINE 1: ..._account" ALTER COLUMN "birthday" TYPE date USING "birthday"...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/almoni/Desktop/Code/my_chaburah/manage.py", line 22, in <module>
main()
File "/Users/almoni/Desktop/Code/my_chaburah/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/core/management/base.py", line 414, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/core/management/base.py", line 460, in execute
output = self.handle(*args, **options)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/core/management/base.py", line 98, in wrapped
res = handle_func(*args, **kwargs)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 290, in handle
post_migrate_state = executor.migrate(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/migrations/executor.py", line 131, in migrate
state = self._migrate_all_forwards(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/migrations/executor.py", line 163, in _migrate_all_forwards
state = self.apply_migration(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/migrations/executor.py", line 248, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/migrations/migration.py", line 131, in apply
operation.database_forwards(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/migrations/operations/fields.py", line 235, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 747, in alter_field
self._alter_field(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/postgresql/schema.py", line 231, in _alter_field
super()._alter_field(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 963, in _alter_field
self.execute(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 192, in execute
cursor.execute(sql, params)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/utils.py", line 103, in execute
return super().execute(sql, params)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/almoni/.local/share/virtualenvs/my_chaburah-AiCSV-sC/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "birthday" does not exist
LINE 1: ..._account" ALTER COLUMN "birthday" TYPE date USING "birthday"...
It only appears when I try to either edit an existing User or create a new one.
models.py:
class Account(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
birthday = models.DateTimeField(blank=True, null=True)
def __str__(self):
return self.user
admin.py:
class AccountInline(admin.StackedInline):
model = Account
can_delete = False
verbose_name_plural = 'Accounts'
class CustomUserAdmin(UserAdmin):
inlines = (AccountInline,)
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
My original guess was the error was due to the fact that my existing Users have no birthday but that doesn't explain why I can't create a new User. Which makes me think I am unaware of what the actual problem is.
I'm newish to django/SQl so I don't really understand the error itself.
You forgot about:
python manage.py makemigrations
# and/or
python manage.py migrate
If error still occurs - delete the database, create new and then run above commands. If got still same error - delete the database and all migration files and then run the commands.
PS You probably want DateField not DateTimeField for birthday storage :)

Django no such table after migrations

After trying all sort of migration im still getting this error
I only get this error when trying to save the new object
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\Carlos\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\base.py", line 741, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\Carlos\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\base.py", line 779, in save_base
force_update, using, update_fields,
File "C:\Users\Carlos\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\base.py", line 870, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Users\Carlos\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\base.py", line 908, in _do_insert
using=using, raw=raw)
File "C:\Users\Carlos\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Carlos\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 1186, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\Carlos\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 1332, in execute_sql
cursor.execute(sql, params)
File "C:\Users\Carlos\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 99, in execute
return super().execute(sql, params)
File "C:\Users\Carlos\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Carlos\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Carlos\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Carlos\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Carlos\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Carlos\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\sqlite3\base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: desporto_noticia
Models
import datetime
from django.db import models
from django.utils import timezone
class Noticia(models.Model):
noticia_texto = models.TextField()
noticia_imagem_path = models.CharField(max_length=100)
noticia_titulo = models.CharField(max_length=100, default='Default')
pub_data = models.DateTimeField('data de publicacao')
noticia_tema = models.CharField(max_length=100)
def __str__(self):
return self.noticia_texto
class Comentario(models.Model):
noticia = models.ForeignKey(Noticia, on_delete=models.CASCADE)
comentario_texto = models.CharField(max_length=300)
def __str__(self):
return self.comentario_texto
Went on my database software to find this is the only table non-existence, the other model has a table tables
Tried all migrates ex :
python manage.py makemigrations desporto
python manage.py sqlmigrate desporto 0001
python manage.py migrate
python manage.py migrate --run-syncdb
Your migrations for the Noticia model has been recorded in the Django migrations table but deleted from the database. If it is not in production you can drop the database and rerun the migrations with the fresh database schema. If you don't want to drop the database then you can delete the migration entry from the migrations table and rerun the migrate command.
Notice: Please don't experiment it with the production database.

Django OperationalError: no such table

I've ran all the commands under the sun to try and fix this which normally solve the problem but this time nothing is working.
python manage.py makemigrations
python manage.py migrate
python manage.py migrate --run-syncdb
python manage.py migrate --fake users
python manage.py migrate users 0012
I've read a few questions on SO and now think it has to do with something executing immediately when imported so the migration doesn't go through.
The only thing I've changed is added a new model TextSubmission and altered views.py. If someone could show me / identify the problematic code, I would greatly appreciate it.
Running migrations:
Rendering model states... DONE
Unapplying users.0014_auto_20190314_1141...Traceback (most recent call last):
File "/home/user/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/user/env/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 298, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: users_textsubmission
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 "/home/user/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/user/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/user/env/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/user/env/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/home/user/env/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/user/env/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 203, in handle
fake_initial=fake_initial,
File "/home/user/env/lib/python3.6/site-packages/django/db/migrations/executor.py", line 121, in migrate
state = self._migrate_all_backwards(plan, full_plan, fake=fake)
File "/home/user/env/lib/python3.6/site-packages/django/db/migrations/executor.py", line 196, in _migrate_all_backwards
self.unapply_migration(states[migration], migration, fake=fake)
File "/home/user/env/lib/python3.6/site-packages/django/db/migrations/executor.py", line 262, in unapply_migration
state = migration.unapply(state, schema_editor)
File "/home/user/env/lib/python3.6/site-packages/django/db/migrations/migration.py", line 175, in unapply
operation.database_backwards(self.app_label, schema_editor, from_state, to_state)
File "/home/user/env/lib/python3.6/site-packages/django/db/migrations/operations/models.py", line 96, in database_backwards
schema_editor.delete_model(model)
File "/home/user/env/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 290, in delete_model
super().delete_model(model)
File "/home/user/env/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 331, in delete_model
"table": self.quote_name(model._meta.db_table),
File "/home/user/env/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 133, in execute
cursor.execute(sql, params)
File "/home/user/env/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 "/home/user/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/user/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/user/env/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/user/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/user/env/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 298, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: users_textsubmission
models.py
class TextSubmission(models.Model):
text_submission = models.CharField(max_length=50, null=True, blank=True)
user = models.OneToOneField(User, on_delete=models.CASCADE)
class Meta:
ordering = ['text_submission']
def __str__(self):
return self.text_submission
views.py
def profile_view(request):
if request.method == 'POST':
p_form = ProfileUpdateForm(request.POST, instance=request.user)
try:
t_form = TextSubmissionForm(request.POST, instance=request.user.textsubmission)
except TextSubmission.DoesNotExist:
t_form = TextSubmissionForm(request.POST)
if p_form.is_valid() and t_form.is_valid():
p_form.save()
t_form.save()
messages.success(request, f'Your account has been updated!')
return redirect('profile')
else:
p_form = ProfileUpdateForm(instance=request.user.profile)
t_form = TextSubmissionForm(instance=request.user.profile)
context = {
'p_form': p_form,
't_form': t_form,
}
return render(request, 'profile.html', context)

A multiple choice field in a form is throwing exception on resetting database

I'm running into a strange issue when resetting my database. A multiple choice field in a form is throwing exception
Unhandled exception in thread started by .wrapper at 0x7f59df160510>
Traceback (most recent call last):
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: app1_semester
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
......
File "/home/abdullah/PycharmProjects/langs-dept/project1/app1/urls.py", line 3, in
from . import views
File "/home/abdullah/PycharmProjects/langs-dept/project1/app1/views.py", line 10, in
from . import models, forms
File "/home/abdullah/PycharmProjects/langs-dept/project1/app1/forms.py", line 11, in
class AddAssignmentForm(forms.Form):
File "/home/abdullah/PycharmProjects/langs-dept/project1/app1/forms.py", line 14, in AddAssignmentForm
queryset=models.AssignmentType.get_assignment_types(),
File "/home/abdullah/PycharmProjects/langs-dept/project1/app1/models.py", line 180, in get_assignment_types
return AssignmentType.objects.filter(semester=Semester.get_current_semester())
File "/home/abdullah/PycharmProjects/langs-dept/project1/app1/models.py", line 157, in get_current_semester
return semester.first() if semester else None
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/models/query.py", line 260, in __bool__
self._fetch_all()
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/models/query.py", line 1087, in _fetch_all
self._result_cache = list(self.iterator())
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql()
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/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: app1_semester
If I comment below piece of code in forms.py, everything is working fine.
class AddAssignmentForm(forms.Form):
assignments = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple(),queryset=models.AssignmentType.get_assignment_types(),required=False)
The method get_assignment_types() is a static method returning some list of values
models.py
#staticmethod
def get_assignment_types():
return AssignmentType.objects.filter()
I can comment that line and do python manage.py makemigrations, but I wonder what is wrong ? Can someone shed some light?
Error is in this line
AssignmentType.objects.filter(semester=Semester.get_current_semester())
You have to do proper migrations for model Semester