我发送了我的模型的项目代码。另外,关于我遇到的错误类型。我正在向我的项目添加 3 种语言。我修复了 settings.py 和 urls.py。然后我运行 makemigrations 并显示此错误。在这种情况下我该怎么办?你有什么建议给我? 我的型号代码:
from django.urls import reverse
from django.db import models
from django.utils.text import slugify
from ckeditor_uploader.fields import RichTextUploadingField
# Create your models here.
class BaseModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
is_active = models.BooleanField(default=True)
class Meta:
abstract = True
class Category(BaseModel):
name = models.CharField(max_length=255)
slug = models.SlugField(null=True, blank=True)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.name, allow_unicode=True)
return super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse('category_articles', args=[self.slug])
class Tag(BaseModel):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
class Article(BaseModel):
title = models.CharField(max_length=255, unique=True)
slug = models.SlugField(null=True, blank=True)
subtitle = models.CharField(max_length=255)
image = models.FileField(upload_to="articles")
description = RichTextUploadingField()
views = models.PositiveIntegerField(default=0)
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="articles")
tags = models.ManyToManyField(Tag, related_name="articles", null=True)
def __str__(self):
return f" {self.id} {self.title}"
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title, allow_unicode=True)
return super().save(*args, **kwargs)
class Contact(BaseModel):
name = models.CharField(max_length=255)
email = models.EmailField()
message = models.TextField()
def __str__(self):
return self.name
显示此错误:
Traceback (most recent call last):
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\django\db\models\options.py", line 681, in get_field
return self.fields_map[field_name]
KeyError: 'n'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\OrderProjects\Omonatstyle\manage.py", line 22, in <module>
main()
File "D:\OrderProjects\Omonatstyle\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
utility.execute()
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\django\core\management\__init__.py", line 416, in execute
django.setup()
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\django\apps\registry.py", line 124, in populate
app_config.ready()
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\modeltranslation\apps.py", line 11, in ready
handle_translation_registrations()
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\modeltranslation\models.py", line 82, in handle_translation_registrations
autodiscover()
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\modeltranslation\models.py", line 28, in autodiscover
import_module(module)
File "C:\Users\SWORD\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "D:\OrderProjects\Omonatstyle\news\translation.py", line 5, in <module>
class CategoryTranslationOptions(TranslationOptions):
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\modeltranslation\decorators.py", line 35, in wrapper
translator.register(model_or_iterable, opts_class, **options)
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\modeltranslation\translator.py", line 538, in register
self._register_single_model(model, opts)
self._register_single_model(model, opts)
self._register_single_model(model, opts)
self._register_single_model(model, opts)
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\modeltranslation\translator.py", line 555, in _register_single_model
self._register_single_model(model, opts)
self._register_single_model(model, opts)
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\modeltranslation\translator.py", line 555, in _register_single_model
self._register_single_model(model, opts)
self._register_single_model(model, opts)
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\modeltranslation\translator.py", line 555, in _register_single_model
self._register_single_model(model, opts)
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\modeltranslation\translator.py", line 555, in _register_single_model
self._register_single_model(model, opts)
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\modeltranslation\translator.py", line 555, in _register_single_model
self._register_single_model(model, opts)
self._register_single_model(model, opts)
self._register_single_model(model, opts)
self._register_single_model(model, opts)
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\modeltranslation\translator.py", line 555, in _register_single_model
add_translation_fields(model, opts)
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\modeltranslation\translator.py", line 199, in add_translation_fields
translation_field = create_translation_field(
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\modeltranslation\fields.py", line 79, in create_translation_field
field = cast(fields.Field, model._meta.get_field(field_name))
File "D:\OrderProjects\Omonatstyle\venv\lib\site-packages\django\db\models\options.py", line 683, in get_field
raise FieldDoesNotExist(
django.core.exceptions.FieldDoesNotExist: Category has no field named 'n'
我发送了我的模型的项目代码。另外,关于我遇到的错误类型。我正在向我的项目添加 3 种语言。我修复了 settings.py 和 urls.py。然后我运行 makemigrations 并显示此错误。在这种情况下我该怎么办?你给我什么建议?
您可能以错误的方式配置了模型翻译。您应该将字段包装到像元组或列表这样的集合中,而不是:
from modeltranslation.translator import TranslationOptions, register
@register(MyModel)
class MyTranslation(TranslationOptions):
fields = 'name'
您使用:
from modeltranslation.translator import TranslationOptions, register
@register(MyModel)
class MyTranslation(TranslationOptions):
fields = ('name',)