django:如何使数据库中的表列具有默认值或以编程方式获取默认值

时间:2011-09-13 19:07:53

标签: sql database django bulkinsert django-south

我有以下型号:

class League(models.Model):
    sport = models.ForeignKey(Sport)
    name = models.CharField(max_length=100, unique=True)
    show = models.BooleanField(default=False)
    display_name = models.CharField(max_length=100, blank=True, default="")

    #display with smallest numbers first.
    display_order = models.IntegerField(default=1000)

但数据库列没有默认值:

sportsite=> \d app_league
                                    Table "public.app_league"
    Column     |          Type          |                        Modifiers
---------------+------------------------+---------------------------------------------------------
 id            | integer                | not null default nextval('app_league_id_seq'::regclass)
 name          | character varying(100) | not null
 show          | boolean                | not null
 display_name  | character varying(100) | not null
 sport_id      | integer                | not null
 display_order | integer                | not null
Indexes:
    "app_league_pkey" PRIMARY KEY, btree (id)
    app_league_name_uniq" UNIQUE, btree (name)
    app_league_sport_id" btree (sport_id)
Foreign-key constraints:
    "sport_id_refs_id_27250fc5" FOREIGN KEY (sport_id) REFERENCES app_sport(id) DEFERRABLE INITIALLY DEFERRED

这是一个问题,因为我想使用批量插入,例如通过制定如下所示的查询:

INSERT INTO app_league (sport_id, name)
(
    SELECT i.sport_id, i.name
    FROM (VALUES (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s
, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s
, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s
, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s
, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s
, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s
, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s
, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s
, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s
, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s
, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s
, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s
, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s, %s), (%s
, %s), (%s, %s), (%s, %s), (%s, %s)) AS i(sport_id, name)
    LEFT JOIN app_league as existing
      ON (existing.sport_id = i.sport_id AND existing.name = i.name)
    WHERE existing.id IS NULL
)

如果(sport_id, league)组合不存在,我只想插入它,并使用django中指定的默认值。如果默认值在SQL数据库中,那么看起来这个查询就可以了。 / p>

那么:有没有办法让django在sql表上创建默认值? (我也在使用南方,如果这有所不同)。

如果没有..我想我可以修改我的查询,只检查与ON重要的字段,但是要在VALUES部分中指定默认值...在这种情况下,如何以编程方式获取字段的默认值,以便我的批量插入代码不需要复制这些值?

1 个答案:

答案 0 :(得分:0)

Django不支持将默认值传递给数据库后端(请参阅#470 "default" values should be expressed in SQL schema)。但是,您应该能够在custom sql file中手动发出update table命令,该命令会安装列的默认值。