python - 带有模型继承的 Django 索引

标签 python django python-3.x django-models django-inheritance

我正在尝试为我的抽象基类模型添加索引,以便它的子类也可以具有相同的索引。

首先我有这个:

class Test(models.Model):
    id = models.BigAutoField(primary_key=True)
    class Meta:
        abstract = True
        indexes = models.Index(fields=['-id'])

class Testing1(Test):
    pass

class Testing2(Test):
    pass

回溯:

  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\yoom\Code\test\indexTestingVel\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\yoom\Code\test\indexTestingVel\venv\lib\site-packages\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "C:\Users\yoom\Code\test\indexTestingVel\venv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\yoom\Code\test\indexTestingVel\venv\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Users\yoom\Code\test\indexTestingVel\venv\lib\site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\yoom\Code\test\indexTestingVel\venv\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:\Users\yoom\Code\test\indexTestingVel\public\models.py", line 4, in <module>
    class Test(models.Model):
  File "C:\Users\yoom\Code\test\indexTestingVel\venv\lib\site-packages\django\db\models\base.py", line 305, in __new__
    new_class._meta.indexes = [copy.deepcopy(idx) for idx in new_class._meta.indexes]
TypeError: 'Index' object is not iterable

这就是我接下来尝试的,只是得到完全相同的错误:

class Test(models.Model):
    id = models.AutoField(primary_key=True)

    class Meta:
        abstract = True


class Testing1(Test):
    class Meta:
        indexes = models.Index(fields=['-id'])


class Testing2(Test):
    class Meta:
        indexes = models.Index(fields=['-id'])

不知道该怎么做,因为我有不止两个子类,并且抽象模型有超过 20 列。

最佳答案

名称indexes [Django-doc]是复数,它需要一个 Index objects [Django-doc] 的列表:

A list of indexes that you want to define on the model

因此,您不应分配 Index 对象本身,而是分配 Index 对象的列表:

class Test(models.Model):
    id = models.BigAutoField(primary_key=True)
    class Meta:
        abstract = True
        indexes = <b>[</b>models.Index(fields=['-id'])<b>]</b>

关于python - 带有模型继承的 Django 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56305495/

相关文章:

python - 无法在 Mac OS X 10.7 上的 virtualenv 中使用 pip 安装 psycopg2

python-3.x - 使用 tf.pad() 填充 MNIST 数据集

python-3.x - 将 stdout 重定向到日志文件,同时仍然打印到 stdout?

python - App Engine Dev Server 数据存储更新速度不够快?

python - 如何在 Python 中使用 win32gui 模块?

python - 在 django ImageField 的错误目录中上传图像

django - Wagtail 管理站点和 Django 管理站点之间的区别?

python - 在 django 模板中显示外键值

python - WTForms 中的 Mongoengine、Flask 和 ReferenceField

python-3.x - Cython 相对导入错误,即使在进行绝对导入时也是如此