模型元选项:使用db_table

时间:2019-05-08 18:27:40

标签: django django-models

我在模型类上使用元类选项db_table以便格式化Django应用 accounts / models.py 中的表名。当我这样做并运行迁移(makemigrations然后迁移)时,它没有附带应用标签(应用名称在我的INSTALLED_APPS列表中)。我想获取<app_label>_<db_table>格式的表。

如果我尝试将app_label选项与db_table一起显式添加到Meta类,则仍不能将其识别为迁移计划的一部分,因此永远不会将其作为{{ 1}}。参考文档和Django 2.2版:https://docs.djangoproject.com/en/2.2/ref/models/options/

project / settings.py

<app_label>_<db_table>

accounts / models.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts.apps.AccountsConfig',
]

帐户/迁移/001_initial.py

class PasswordAttempt(models.Model):
    attempt_id = models.IntegerField(primary_key=True)
    security_id = models.ForeignKey(Security, on_delete=models.CASCADE)
    attempt_count = models.IntegerField(default=0)

    class Meta:
        app_label = 'accounts' # Shouldn't be necessary, but still doesn't work
        db_table = 'password_attempt'

在我进入Meta类的class Migration(migrations.Migration): initial = True dependencies = [ ] ...# Other models migrations.CreateModel( name='PasswordAttempt', fields=[ ('attempt_id', models.IntegerField(primary_key=True, serialize=False)), ('attempt_count', models.IntegerField(default=0)), ('security_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='accounts.Security')), ], options={ 'db_table': 'password_attempt', }, ), ] 之前,一切似乎都很好。一旦发生这种情况,我将失去该表名称的应用标签。我可能会遗漏一些明显的东西,但是在这里我的轮胎有些旋转。任何想法都将不胜感激。

0 个答案:

没有答案