Issue
Django 3.0.4
def get_choices():
blank_choice = (None, '----')
choices = []
# choices = [blank_choice] + [(str(item.id), str(item)) for item in YandexTextAdTemplates.objects.all()]
return choices
class CopyForm(forms.Form):
ad_template = forms.ChoiceField(choices=get_choices())
I need choices in my form. Now the necessary string is commented out. If I return choices back, it will work. But it will not make migrations.
(venv) michael@michael:~/PycharmProjects/ads3/ads_manager$ python manage.py makemigrations
Traceback (most recent call last):
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "yandex_ads_yandextextadtemplates" does not exist
LINE 1: ...ndextextadtemplates"."audience_yandex_pixel" FROM "yandex_ad...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/core/management/base.py", line 366, in execute
self.check()
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/core/management/base.py", line 392, in check
all_issues = self._run_checks(
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/core/management/base.py", line 382, in _run_checks
return checks.run_checks(**kwargs)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 407, in check
for pattern in self.url_patterns:
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 588, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 581, in urlconf_module
return import_module(self.urlconf_name)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/michael/PycharmProjects/ads3/ads_manager/ads_manager/urls.py", line 35, in <module>
path('yandex_ads/', include("yandex_ads.urls", namespace="yandex_ads"), name="yandex_ads"),
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/michael/PycharmProjects/ads3/ads_manager/yandex_ads/urls.py", line 3, in <module>
from .views import RequestYandexAds, ShowYandexAds, CreateYandexAds, CopyAdTemplate
File "/home/michael/PycharmProjects/ads3/ads_manager/yandex_ads/views.py", line 6, in <module>
from .forms import CopyForm
File "/home/michael/PycharmProjects/ads3/ads_manager/yandex_ads/forms.py", line 12, in <module>
class CopyForm(forms.Form):
File "/home/michael/PycharmProjects/ads3/ads_manager/yandex_ads/forms.py", line 13, in CopyForm
ad_template = forms.ChoiceField(choices=get_choices())
File "/home/michael/PycharmProjects/ads3/ads_manager/yandex_ads/forms.py", line 8, in get_choices
choices = [blank_choice] + [(str(item.id), str(item)) for item in YandexTextAdTemplates.objects.all()]
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/db/models/query.py", line 276, in __iter__
self._fetch_all()
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/db/models/query.py", line 1261, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/db/models/query.py", line 57, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1151, in execute_sql
cursor.execute(sql, params)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/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/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "yandex_ads_yandextextadtemplates" does not exist
LINE 1: ...ndextextadtemplates"."audience_yandex_pixel" FROM "yandex_ad...
So, I commented the line out, organized empty list for choices. Migrations have been made.
But it I need the correct functionality and possibility to make migrations. What can I try next?
Solution
You cannot (at the very least should not) evaluate Querysets at module level which is what happens in your case as you call the get_choices
function in your field definition:
ad_template = forms.ChoiceField(choices=get_choices())
Pass it a callable instead:
ad_template = forms.ChoiceField(choices=get_choices)
This way, the choices are built dynamically every time the form is instantiated which is what you actually want.
See the docs on the choices
parameter.
Answered By - user2390182
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.