【问题标题】:When to use auto_created attribute of field in django?何时在 django 中使用字段的 auto_created 属性?
【发布时间】:2016-06-09 16:35:15
【问题描述】:

你能否给我一个真实的例子,我什么时候应该在 django 模型字段的定义中使用 auto_created 属性? https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.Field.auto_created ?使用这个标志可以解决哪些问题?

【问题讨论】:

  • 我也在找这个,奇怪的是我找不到任何例子。

标签: django-models django-orm


【解决方案1】:

在这里找到一个例子。 http://www.programcreek.com/python/example/74720/django.db.migrations.CreateModel

这是我的猜测:它是为 Django 主键实现而设计的,也就是说,除非你正在开发 Django,否则最好不要使用它。

fields=[
    ('pony_ptr', models.OneToOneField(
        auto_created=True,
        primary_key=True,
        to_field='id',
        serialize=False,
        to='Pony',
    )),
    "cuteness", models.IntegerField(default=1)),
]

【讨论】:

    猜你喜欢
    • 2016-03-27
    • 1970-01-01
    • 2013-12-26
    • 2013-06-19
    • 2023-03-25
    • 2011-06-06
    • 2014-07-07
    • 2015-03-12
    • 1970-01-01
    相关资源
    最近更新 更多