I have the following model:
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)
But the database columns have no defaults:
sportsite=> \d app_league
Table "public.app_league"
Column | Type | Modifiers
---------------+------------------------+---------------------------------------------------------
id | integer | not null default nextval('app_league_id_seq'::regclass)
name | characte开发者_高级运维r 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
This is a problem, because I want to use bulk inserts, e.g. by formulating a query that looks like this:
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
)
I only want to insert it if the (sport_id, league)
combination doesn't exist, with the default values as specified in django.. if the defaults were in the SQL database, then it seems this query would work.
So: is there a way to get django to create defaults on the sql tables? (I'm also using south, if that makes a difference).
If not.. I suppose I can modify my query to only check for the fields that matter with the ON
, but to specify the defaults in the VALUES
part... in that case, how can I get the default value of a field programmatically, so that my bulk-insert code doesn't need to duplicate those values?
Django does not support passing the default value to the database backend (see #470 "default" values should be expressed in SQL schema). However, you should be able to manually issue an update table
command in a custom sql file, that installs the default value for the column.
精彩评论