<small id='8jpo6'></small><noframes id='8jpo6'>

    1. <tfoot id='8jpo6'></tfoot>
      • <bdo id='8jpo6'></bdo><ul id='8jpo6'></ul>
      1. <i id='8jpo6'><tr id='8jpo6'><dt id='8jpo6'><q id='8jpo6'><span id='8jpo6'><b id='8jpo6'><form id='8jpo6'><ins id='8jpo6'></ins><ul id='8jpo6'></ul><sub id='8jpo6'></sub></form><legend id='8jpo6'></legend><bdo id='8jpo6'><pre id='8jpo6'><center id='8jpo6'></center></pre></bdo></b><th id='8jpo6'></th></span></q></dt></tr></i><div id='8jpo6'><tfoot id='8jpo6'></tfoot><dl id='8jpo6'><fieldset id='8jpo6'></fieldset></dl></div>

      2. <legend id='8jpo6'><style id='8jpo6'><dir id='8jpo6'><q id='8jpo6'></q></dir></style></legend>

        运行 make 迁移时出现 Django 关系错误

        时间:2023-11-08
          <bdo id='T96Hv'></bdo><ul id='T96Hv'></ul>

              <tbody id='T96Hv'></tbody>

            <small id='T96Hv'></small><noframes id='T96Hv'>

              <i id='T96Hv'><tr id='T96Hv'><dt id='T96Hv'><q id='T96Hv'><span id='T96Hv'><b id='T96Hv'><form id='T96Hv'><ins id='T96Hv'></ins><ul id='T96Hv'></ul><sub id='T96Hv'></sub></form><legend id='T96Hv'></legend><bdo id='T96Hv'><pre id='T96Hv'><center id='T96Hv'></center></pre></bdo></b><th id='T96Hv'></th></span></q></dt></tr></i><div id='T96Hv'><tfoot id='T96Hv'></tfoot><dl id='T96Hv'><fieldset id='T96Hv'></fieldset></dl></div>
            1. <legend id='T96Hv'><style id='T96Hv'><dir id='T96Hv'><q id='T96Hv'></q></dir></style></legend>

                <tfoot id='T96Hv'></tfoot>
                • 本文介绍了运行 make 迁移时出现 Django 关系错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  Hey I am attempting to initialize a new database, but I am running into some issues setting up the migrations. The error I am getting appears to stem from setting up my forms. In a form I am using, I am creating a choice field as so:

                  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 来自这里:

                  where PROVINCE_CHOICES comes from here:

                  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))
                  

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

                  The issue seems to be that this loop is being called before the migrations occur, giving me an error stating that the Province model does not exist. Commenting out the reference to this file allows the migrations to work, however, that seems like an impractical solution for continued use. Is there a way to get around this error?

                  作为参考,这是我在运行 manage.py makemigrations 时遇到的错误:

                  For reference, here is the error I get when I run 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)
                  

                  推荐答案

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

                  You cannot execute queries during the initialization of the app registry. Your choices.py file is indirectly imported during this time, resulting in the error. To fix this issue, you can pass a callable to 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)
                  

                  这篇关于运行 make 迁移时出现 Django 关系错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 django admin 中保存对象时出现 unicode 错误 下一篇:django-admin.py 不工作

                  相关文章

                    <tfoot id='WLI8Y'></tfoot>

                    <i id='WLI8Y'><tr id='WLI8Y'><dt id='WLI8Y'><q id='WLI8Y'><span id='WLI8Y'><b id='WLI8Y'><form id='WLI8Y'><ins id='WLI8Y'></ins><ul id='WLI8Y'></ul><sub id='WLI8Y'></sub></form><legend id='WLI8Y'></legend><bdo id='WLI8Y'><pre id='WLI8Y'><center id='WLI8Y'></center></pre></bdo></b><th id='WLI8Y'></th></span></q></dt></tr></i><div id='WLI8Y'><tfoot id='WLI8Y'></tfoot><dl id='WLI8Y'><fieldset id='WLI8Y'></fieldset></dl></div>
                    1. <legend id='WLI8Y'><style id='WLI8Y'><dir id='WLI8Y'><q id='WLI8Y'></q></dir></style></legend>
                        <bdo id='WLI8Y'></bdo><ul id='WLI8Y'></ul>

                    2. <small id='WLI8Y'></small><noframes id='WLI8Y'>