What methods do you use to set up database level defaults in django?
I understand this is a django wontfix decision, but there must be some ideas floating around about a way to implement this if written for a specific backend (postgres).
I’d like to power this through python so I’d prefer not to use /sql/.sql files.
I was thinking I might set attributes on existing fields such as:
foo = models.CharField(max_length=256, blank=True)
foo.db_default = ''
Then filter through fields and construct an ALTER COLUMN SET DEFAULT query.
Where would I put that though? The docs say not to alter the database in the post_syncdb hook.
Any ideas where to put this action? I’m also thinking I could generate the sql/.sql files dynamically. A management command would be better than nothing. Automatic would be great. Hmm I wonder if post_syncdb can generate sql files in time to be executed? Will test.
I tried doing
post_syncdbbut it was too late to use .sql files. Hooking intoclass_preparedworked out in time forsyncdbbut I realized I was overcomplicating things. I might as well run some code once at model instantiation.I ended up with a straight forward solution that can be easily understood in 6 months when I need to fiddle with this again.