使用 'unique' 的翻译字段会引发 NameError,django-parler
Using translate field for 'unique' rises NameError, django-parler
我最近安装了 django-parler==2.2
,当我重写我的模型以继承 TranslatableModel
然后尝试 makemigrations
我收到以下错误:
main_menu_slug = models.SlugField(max_length=120, unique=main_menu_item)
NameError: name 'main_menu_item' is not defined
当我从 models.Model
继承时,一切都运行良好。这是代码:
class MenuItem(TranslatableModel):
translations = TranslatedFields (
main_menu_item = models.CharField(max_length=60),
main_menu_slug = models.SlugField(max_length=120, unique=main_menu_item)
)
我刚刚开始使用这个模块,所以可能是一些微不足道的事情,但我找不到解决方案。我做错了什么?
完整追溯:
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 "C:\project\alfa\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\project\alfa\lib\site-packages\django\core\management\__init__.py", line 377, in execute
django.setup()
File "C:\project\alfa\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\project\alfa\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\project\alfa\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\project\alfa\project\menus\models.py", line 16, in <module>
class MenuItem(TranslatableModel):
File "C:\project\alfa\project\menus\models.py", line 20, in MenuItem
main_menu_slug = models.SlugField(max_length=120, unique=main_menu_item)
NameError: name 'main_menu_item' is not defined
工作代码models.Model
:
class MenuItem(models.Model):
main_menu_item = models.CharField(max_length=60)
main_menu_slug = models.SlugField(max_length=120, unique=main_menu_item)
根据文档,unique
应该是一个布尔值:https://docs.djangoproject.com/en/3.1/ref/models/fields/#unique
我最近安装了 django-parler==2.2
,当我重写我的模型以继承 TranslatableModel
然后尝试 makemigrations
我收到以下错误:
main_menu_slug = models.SlugField(max_length=120, unique=main_menu_item)
NameError: name 'main_menu_item' is not defined
当我从 models.Model
继承时,一切都运行良好。这是代码:
class MenuItem(TranslatableModel):
translations = TranslatedFields (
main_menu_item = models.CharField(max_length=60),
main_menu_slug = models.SlugField(max_length=120, unique=main_menu_item)
)
我刚刚开始使用这个模块,所以可能是一些微不足道的事情,但我找不到解决方案。我做错了什么?
完整追溯:
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 "C:\project\alfa\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\project\alfa\lib\site-packages\django\core\management\__init__.py", line 377, in execute
django.setup()
File "C:\project\alfa\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\project\alfa\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\project\alfa\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\project\alfa\project\menus\models.py", line 16, in <module>
class MenuItem(TranslatableModel):
File "C:\project\alfa\project\menus\models.py", line 20, in MenuItem
main_menu_slug = models.SlugField(max_length=120, unique=main_menu_item)
NameError: name 'main_menu_item' is not defined
工作代码models.Model
:
class MenuItem(models.Model):
main_menu_item = models.CharField(max_length=60)
main_menu_slug = models.SlugField(max_length=120, unique=main_menu_item)
unique
应该是一个布尔值:https://docs.djangoproject.com/en/3.1/ref/models/fields/#unique