我正在用Django做一个博客应用,我想修改Post类的一个字段:
我想将 meta_description
字段更改为仅 description
。
来自:
class Post(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField(auto_now_add=True)
meta_description = models.TextField(blank=True, null=True)
body = models.TextField()
def __str__(self):
"""
Show the title and the author in the admin Page
"""
return self.title + " by " + str(self.author)
def get_absolute_url(self):
return reverse("blog:article_page", kwargs={"pk": self.pk})
⏎
致:
class Post(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField(auto_now_add=True)
description = models.TextField(blank=True, null=True)
body = models.TextField()
def __str__(self):
"""
Show the title and the author in the admin Page
"""
return self.title + " by " + str(self.author)
def get_absolute_url(self):
return reverse("blog:article_page", kwargs={"pk": self.pk})
当我在模型中这样做时:我有一个错误进行迁移:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/daniel/MEGA/my-git/github/Developer-road-website/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/daniel/MEGA/my-git/github/Developer-road-website/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/daniel/MEGA/my-git/github/Developer-road-website/venv/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/daniel/MEGA/my-git/github/Developer-road-website/venv/lib/python3.8/site-packages/django/core/management/base.py", line 368, in execute
self.check()
File "/home/daniel/MEGA/my-git/github/Developer-road-website/venv/lib/python3.8/site-packages/django/core/management/base.py", line 392, in check
all_issues = checks.run_checks(
File "/home/daniel/MEGA/my-git/github/Developer-road-website/venv/lib/python3.8/site-packages/django/core/checks/registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/home/daniel/MEGA/my-git/github/Developer-road-website/venv/lib/python3.8/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/daniel/MEGA/my-git/github/Developer-road-website/venv/lib/python3.8/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/home/daniel/MEGA/my-git/github/Developer-road-website/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "/home/daniel/MEGA/my-git/github/Developer-road-website/venv/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/daniel/MEGA/my-git/github/Developer-road-website/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/daniel/MEGA/my-git/github/Developer-road-website/venv/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/daniel/MEGA/my-git/github/Developer-road-website/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/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/daniel/MEGA/my-git/github/Developer-road-website/DeveloperRode/DeveloperRode/urls.py", line 26, in <module>
path("blog/", include('blog.urls')),
File "/home/daniel/MEGA/my-git/github/Developer-road-website/venv/lib/python3.8/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/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/daniel/MEGA/my-git/github/Developer-road-website/DeveloperRode/blog/urls.py", line 2, in <module>
from .views import BlogView, ArticleDetail, PostCreateView, EditPost, PostDeleteView
File "/home/daniel/MEGA/my-git/github/Developer-road-website/DeveloperRode/blog/views.py", line 8, in <module>
from .forms import PostForm, EditForm
File "/home/daniel/MEGA/my-git/github/Developer-road-website/DeveloperRode/blog/forms.py", line 4, in <module>
class PostForm(forms.ModelForm):
File "/home/daniel/MEGA/my-git/github/Developer-road-website/venv/lib/python3.8/site-packages/django/forms/models.py", line 268, in __new__
raise FieldError(message)
我能做什么?
答案 0 :(得分:1)
发生这种情况的原因是因为除了模型之外,您可能还有仍然使用 meta_description
的表单、视图、序列化程序等。如果您因此有一个 PostForm
喜欢:
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ['meta_description']
# change to description ↑
那么您也应该将其更改为 description
。因此,您应该在您的项目中使用 meta_description
执行搜索并更改 meta_description
的出现次数,但要小心:并非所有 meta_description
都参考Post
模型的字段,如果是关于另一个模型,你当然不应该修改它。
答案 1 :(得分:1)
我解决了这个问题,这似乎是一个表单问题,我在那里调用了旧字段 meta_description。
我在 forms.py
文件中更正它并执行 makemigrations
命令,之后它显示一个提示:
Did you rename post.meta_description to post.description (a TextField)? [y/N] y
现在问题解决了。