我知道这个问题已经被问过了,但是我已经尝试了答案中建议的所有内容,但似乎都没有。
问题:我有一个名为' Accomm
'的模型类。在我的Django项目中,我存储了许多对象。在我尝试检索一个或多个' Accomm
'的实例的网页上然后引发以下错误......
ProgrammingError at /
column objects_accomm.description does not exist
LINE 1: ...omm"."id", "objects_accomm"."mapped_location_id", "objects_a...
当我迁移新字段' description
&#39>时,问题就开始出现了。到了Accomm
'模型。
尝试过的解决方案:我试图做以下事情......清除所有Django迁移(使用--zero和--empty命令);清除数据库(PostgreSQL 10);清除迁移和数据库;在Django项目settings.py文件中更改我的数据库的名称。这些似乎都不起作用。我还关注了教程here,here和here。
完整错误:
Django Version: 1.11.7
Python Version: 2.7.10
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.gis',
'django.contrib.humanize',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.staticfiles',
'rest_framework',
'accounts',
'maps',
'objects',
'social_django']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'social_django.middleware.SocialAuthExceptionMiddleware']
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/jayt/project/main/views.py" in home
21. return render(request, 'home.html', {'accomm':accomm})
File "/Library/Python/2.7/site-packages/django/shortcuts.py" in render
30. content = loader.render_to_string(template_name, context, request, using=using)
File "/Library/Python/2.7/site-packages/django/template/loader.py" in render_to_string
68. return template.render(context, request)
File "/Library/Python/2.7/site-packages/django/template/backends/django.py" in render
66. return self.template.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
207. return self._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
199. return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
177. return compiled_parent._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
199. return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
72. result = block.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File "/Library/Python/2.7/site-packages/django/template/defaulttags.py" in render
321. if match:
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in __nonzero__
258. return type(self).__bool__(self)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in __bool__
254. self._fetch_all()
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in _fetch_all
1118. self._result_cache = list(self._iterable_class(self))
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in __iter__
53. results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
894. raise original_exception
Exception Type: ProgrammingError at /
Exception Value: column objects_accomm.description does not exist
LINE 1: ...omm"."id", "objects_accomm"."mapped_location_id", "objects_a...
^
修改 Models.py适用于' Accomm':
class Accomm(models.Model):
mapped_location=models.ForeignKey(Location,related_name='l_accomms',null=True,blank=True,on_delete=models.SET_NULL)
description=models.CharField(max_length=1000,null=True,blank=True)
creator=models.OneToOneField(accountmodels.UserProfileModel,related_name='u_creator',null=True,on_delete=models.SET_NULL,blank=True)
slug_key=models.SlugField(unique=True)
is_active=models.BooleanField(default=False)
Views.py:
def home(request):
user = request.user
accomm = Accomm.objects.annotate(img_count=models.Count('a_image')).filter(img_count__gte=1)[:6]
return render(request, 'home.html', {'accomm':accomm})
除了这里提出的解决方案之外还有其他任何想法吗?
答案 0 :(得分:0)
当我的迁移变得困难时,我喜欢尝试的东西是:
python manage.py makemigrations
python manage.py migrate --fake
因此,这会使您的模型看起来像已添加的列,因此我从此处将模型剪切并粘贴到临时文件中,保存模型并定期运行迁移,这将删除所有模型。然后将模型粘贴回models.py并再次运行迁移。这似乎对我有用,可能有更好的方法来管理这个,但我对django相对较新,因为我刚刚完成了我的第一个大项目。真的希望这有帮助,如果不让我知道,我可能还有别的东西给你!