运行make迁移时的Django关系错误

书面

嘿,我正在尝试初始化一个新数据库,但是在设置迁移时遇到了一些问题。我收到的错误似乎源于设置表单。在我使用的表单中,我正在这样创建一个选择字段:

from django import forms
from ..custom_admin import widgets, choices


class MemberForm(forms.Form):
    provinces = forms.ChoiceField(label='Provinces', choices=choices.PROVINCE_CHOICES, required=True)

PROVINCE_CHOICES来自哪里:

from ..base.models import ProvinceCode

PROVINCE_CHOICES = []
for province in ProvinceCode.objects.filter(country_code_id=1).order_by('code'):
    PROVINCE_CHOICES.append((province.code, province.code))

问题似乎是在迁移发生之前就调用了此循环,这给我一个错误,指出省模型不存在。注释掉对该文件的引用可以使迁移工作,但是,这对于继续使用似乎是不切实际的解决方案。有没有办法解决这个错误?

供参考,这是我运行时遇到的错误manage.py makemigrations

./manage.py makemigrations        
Traceback (most recent call last):
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "pc_psr_code" does not exist
LINE 1: ...escription", "pc_psr_code"."country_code_id" FROM "pc_psr_co...
                                                             ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./manage.py", line 9, in <module>
    django.setup()
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/debug_toolbar/apps.py", line 15, in ready
    dt_settings.patch_all()
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/debug_toolbar/settings.py", line 228, in patch_all
    patch_root_urlconf()
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/debug_toolbar/settings.py", line 216, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 568, in reverse
    app_list = resolver.app_dict[ns]
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 360, in app_dict
    self._populate()
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 293, in _populate
    for pattern in reversed(self.url_patterns):
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/Users/js/Documents/app/platform/test/pc/urls.py", line 7, in <module>
    from .custom_admin import urls as custom_urls
  File "/Users/js/Documents/app/platform/test/pc/custom_admin/urls.py", line 3, in <module>
    from ..party import views as party_views
  File "/Users/js/Documents/app/platform/test/pc/party/views.py", line 1, in <module>
    from ..party import forms
  File "/Users/js/Documents/app/platform/test/pc/party/forms.py", line 2, in <module>
    from ..custom_admin import widgets, choices
  File "/Users/js/Documents/app/platform/test/pc/custom_admin/choices.py", line 9, in <module>
    for province in ProvinceCode.objects.filter(country_code_id=1).order_by('code'):
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/models/query.py", line 258, in __iter__
    self._fetch_all()
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/models/query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/models/query.py", line 52, in __iter__
    results = compiler.execute_sql()
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
    cursor.execute(sql, params)
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "pc_psr_code" does not exist
LINE 1: ...escription", "pc_psr_code"."country_code_id" FROM "pc_psr_co...

省份型号:

class ProvinceCode(models.Model):
    code = models.CharField(blank=False, null=False, unique=True)
    country_code = models.ForeignKey('CountryCode', blank=False, null=True)
n

您无法在应用程序注册表初始化期间执行查询。您的choices.py文件在此期间被间接导入,从而导致错误。要解决此问题,您可以将Callable传递给choices

def get_provinces():
    province_choices = []
    for province in ProvinceCode.objects.filter(country_code_id=1).order_by('code'):
        province_choices.append((province.code, province.code))
    return province_choices

class MemberForm(forms.Form):
    provinces = forms.ChoiceField(label='Provinces', choices=get_provinces, required=True)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Django 1.7c2中运行迁移时出现循环依赖项错误

来自分类Dev

运行迁移时如何解决外键错误

来自分类Dev

运行Laravel 4迁移时出现SQL 1005错误

来自分类Dev

django:如何避免迁移时出现权限错误

来自分类Dev

尝试在Django中迁移时出现奇怪的错误

来自分类Dev

运行ecto迁移时出错

来自分类Dev

运行迁移时的狂欢问题

来自分类Dev

运行ecto迁移时出错

来自分类Dev

迁移时Django FieldDoesNotExist异常

来自分类Dev

迁移时的python django问题

来自分类Dev

运行Yii用户扩展的迁移时出现“迁移目录不存在”错误

来自分类Dev

在Django中的mysql中迁移时如何解决迁移中的错误

来自分类Dev

在Django上应用迁移时,“关系“ social_auth_code”不存在”

来自分类Dev

在Django上应用迁移时,“关系“ social_auth_code”不存在”

来自分类Dev

Django在应用程序启动时运行代码,但在迁移时不运行

来自分类Dev

运行数据库迁移时出现未定义的方法“ each_pair”错误

来自分类Dev

LARAVEL 8:常规错误:使用外键运行迁移时发生了1005

来自分类Dev

运行Symfony迁移时添加数据

来自分类Dev

在运行显式创建表的迁移时,未创建Django表。

来自分类Dev

Django:运行数据迁移时,“列<whatever>不存在”

来自分类Dev

ImportError:为Django邀请运行迁移时,无法导入名称“ python_2_unicode_compatible”

来自分类Dev

如何避免迁移时出现错误1146?

来自分类Dev

使用查询数据库的默认字段函数进行迁移时出现Django错误

来自分类Dev

压缩Django迁移时的循环依赖

来自分类Dev

Django-在每次迁移时插入行

来自分类Dev

预构建的 django 项目在迁移时出错

来自分类Dev

在执行核心数据迁移时如何获取关系实体?

来自分类Dev

当我尝试在 django 中进行迁移时,我收到未定义 DJANGO_SETTINGS_MODULE 的错误

来自分类Dev

Heroku:在 django 中迁移时出现“django.db.utils.DataError: value too long for type character variables (20)”错误

Related 相关文章

  1. 1

    在Django 1.7c2中运行迁移时出现循环依赖项错误

  2. 2

    运行迁移时如何解决外键错误

  3. 3

    运行Laravel 4迁移时出现SQL 1005错误

  4. 4

    django:如何避免迁移时出现权限错误

  5. 5

    尝试在Django中迁移时出现奇怪的错误

  6. 6

    运行ecto迁移时出错

  7. 7

    运行迁移时的狂欢问题

  8. 8

    运行ecto迁移时出错

  9. 9

    迁移时Django FieldDoesNotExist异常

  10. 10

    迁移时的python django问题

  11. 11

    运行Yii用户扩展的迁移时出现“迁移目录不存在”错误

  12. 12

    在Django中的mysql中迁移时如何解决迁移中的错误

  13. 13

    在Django上应用迁移时,“关系“ social_auth_code”不存在”

  14. 14

    在Django上应用迁移时,“关系“ social_auth_code”不存在”

  15. 15

    Django在应用程序启动时运行代码,但在迁移时不运行

  16. 16

    运行数据库迁移时出现未定义的方法“ each_pair”错误

  17. 17

    LARAVEL 8:常规错误:使用外键运行迁移时发生了1005

  18. 18

    运行Symfony迁移时添加数据

  19. 19

    在运行显式创建表的迁移时,未创建Django表。

  20. 20

    Django:运行数据迁移时,“列<whatever>不存在”

  21. 21

    ImportError:为Django邀请运行迁移时,无法导入名称“ python_2_unicode_compatible”

  22. 22

    如何避免迁移时出现错误1146?

  23. 23

    使用查询数据库的默认字段函数进行迁移时出现Django错误

  24. 24

    压缩Django迁移时的循环依赖

  25. 25

    Django-在每次迁移时插入行

  26. 26

    预构建的 django 项目在迁移时出错

  27. 27

    在执行核心数据迁移时如何获取关系实体?

  28. 28

    当我尝试在 django 中进行迁移时,我收到未定义 DJANGO_SETTINGS_MODULE 的错误

  29. 29

    Heroku:在 django 中迁移时出现“django.db.utils.DataError: value too long for type character variables (20)”错误

热门标签

归档