Reputation: 3989
Is it possible to create a fixture that is written to the DB using loaddata
, and overwrites some fields of an existing record, but not all of them?
For example, suppose I have a table in my DB called app_foo
:
id | bar | baz |
---|---|---|
1 | "a" | "b" |
2 | "c" | "d" |
The corresponding YAML fixture file for this would look like:
- model: app.foo
pk: 1
fields:
bar: "a"
baz: "b"
- model: app.foo
pk: 2
fields:
bar: "c"
baz: "d"
How would I modify this fixture such that:
baz
field when loading the fixture into a fresh empty database.baz
, they aren't overwritten with NULL
.I thought that I could do something like this:
- model: app.foo
pk: 1
fields:
bar: "a"
- model: app.foo
pk: 2
fields:
bar: "c"
or this:
- model: app.foo
pk: 1
fields:
bar: "a"
baz:
- model: app.foo
pk: 2
fields:
bar: "c"
baz:
But both attempts overwrote the baz
field for DB records 1 and 2 with NULL
. This isn't specified in the documentation for fixtures, but is an empty field in a fixture implicitly NULL
?
Upvotes: 0
Views: 524
Reputation: 15758
As written in documentation you linked
Each time you run loaddata, the data will be read from the fixture and re-loaded into the database. Note this means that if you change one of the rows created by a fixture and then run loaddata again, you’ll wipe out any changes you’ve made.
Django does not set values for fields that are not in fixture but they default to default
field value, which is NULL
in case you set null=true
Upvotes: 2