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

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

本文介绍了运行make迁移时的Django关系错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  from django import form 
from。 .custom_admin导入小部件,选择


类MemberForm(forms.Form):
省=形式。ChoiceField(label =省,choices = choices.PROVINCE_CHOICES,必需=是的)

其中PROVINCE_CHOICES来自此处:



导入Province of Code

PROVINCE_CHOICES = []
for ProvinceCode.objects.filter(country_code_id = 1)中的省份。 (代码):
PROVINCE_CHOICES.append((province.code,province.code))

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



作为参考,这是我运行 manage.py makemigrations

  ./ manage.py makemigrations 
追溯(最近一次通话):
执行
中的文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/backends/utils.py,第64行,返回self.cursor.execute (sql,params)
psycopg2.ProgrammingError:关系 pc_psr_code不存在
第1行:...描述, pc_psr_code。 country_code_id来自 pc_psr_co ...
^


上面的异常是以下异常的直接原因:

追溯(最近一次调用为最新):
文件 ./manage .py,第9行,< module>
django.setup()
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/__init__.py,第18行,在安装程序$ b中$ b apps.populate(settings.INSTALLED_APPS)
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/apps/registry.py,第115行,在填充
app_config.ready()
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/debug_toolbar/apps.py,第15行,已准备好
dt_settings.patch_all()
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/debug_toolbar/settings.py,第228行,在patch_all $ b $中b patch_root_urlconf()
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/debug_toolbar/settings.py,行216,在patch_root_urlconf
reverse(' djdt:render_panel')
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/core/urlresolvers.py,第56行8,反过来
app_list = resolver.app_dict [ns]
File /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/core/urlresolvers.py ,第360行,位于app_dict
self._populate()
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/core/urlresolvers.py ,第293行,在_populate
中用于反转的模式(self.url_patterns):
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/ utils / functional.py,第33行,位于__get__
res = instance .__ dict __ [self.name] = self.func(instance)
文件 / Users / js / Documents / VirtualEnvironments / pcenv / lib / python3.5 / site-packages / django / core / urlresolvers.py,第417行,以url_patterns
模式= getattr(self.urlconf_module, urlpatterns,self.urlconf_module)
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/utils/functional.py,第33行,位于__get__
res = insta nce .__ dict __ [self.name] = self.func(instance)
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/core/urlresolvers.py urlconf_module
中的第410行,return import_module(self.urlconf_name)
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/importlib/__init__.py,第126行,在import_module
中返回_bootstrap._gcd_import(name [level:],程序包,级别)
文件< frozen importlib._bootstrap>,行986,在_gcd_import
文件中< frozen importlib._bootstrap>,第969行,在_find_and_load
文件< frozen importlib._bootstrap>,第958行,在_find_and_load_unlocked
文件< frozen importlib._bootstrap>,第673行, _load_unlocked
文件< frozen importlib._bootstrap_external>,第662行,在exec_module
文件< frozen importlib._bootstrap>,第222行,_call_with_frames_removed
文件 / Users / js / Documents / app / platform / test / pc / urls.py,第7行,在< module>中.custom_admin中的
导入URL作为custom_urls
文件 /Users/js/Documents/app/platform/test/pc/custom_admin/urls.py,第3行,在< module>来自..party的
作为party_views导入视图
文件 /Users/js/Documents/app/platform/test/pc/party/views.py,第1行,< module>来自..party导入表单的

文件 /Users/js/Documents/app/platform/test/pc/party/forms.py,第2行,< module> ..custom_admin导入小部件中的
,选择
文件 /Users/js/Documents/app/platform/test/pc/custom_admin/choices.py,第9行,在< module>省代码
在Province of Code.objects.filter(country_code_id = 1).order_by('code')中:
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-包/django/db/models/query.py,第258行,位于__iter__
self._fetch_all()
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/ site-packages / django / db / models / query.py,行1074,在_fetch_all
self._result_cache = list(self.iterator())
文件 / Users / js / Documents / VirtualEnvironments中/pcenv/lib/python3.5/site-packages/django/db/models/query.py,第52行,位于__iter__
结果= compile.execute_sql()
文件 / Users / js /Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/models/sql/compiler.py,第848行,在execute_sql
cursor.execute(sql,params)$ b $中b在执行
中的文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/backends/utils.py中,第79行return super(CursorDebugWrapper,self )。执行(sql,params)
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/backends/utils.py,执行$ b,位于第64行$ b return self.cursor.execute(sql,params)
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/utils.py,行95,位于__exit__
中。6.reraise(dj_exc_type,dj_exc_value,traceback)
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/utils/six .py,第685行,用于提高
的价值。with_traceback(tb)
文件 /Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db /backends/utils.py,第64行,在执行
中返回self.cursor.execute(sql,params)
django.db.utils.ProgrammingError:关系 pc_psr_code不存在
第1行:...说明, pc_psr_code。 country_code_id来自 pc_psr_co ...

省模型:

  class ProvinceCode(mod els.Model):
代码= models.CharField(空白= False,null = False,唯一= True)
country_code = models.ForeignKey('CountryCode',blank = False,null = True)


解决方案

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

  def get_provinces():
province_choices = []
for 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 = form.ChoiceField(label ='Provinces',choices = get_provinces,required =是的)


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)

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?

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

Province model:

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

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关系错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 07:49