user984003
user984003

Reputation: 29557

Python, MySQL, Django: set default value for table columns

My tables are specified as models in Python, which are then made into MySQL tables by Django. In Python:

class my_table(models.Model):
    is_correct = models.IntegerField(default = 0)

If I do an insert into this table then I want it to automatically insert 0 for the column is_correct unless I specify a different value. I also want this to happen if I am using raw sql or if I am inserting from a MySQL stored procedure.

The default argument only seems to work from within Python. It's not translated into anything that MySQL sees. Is such a thing possible?

Upvotes: 3

Views: 1540

Answers (1)

San4ez
San4ez

Reputation: 8241

Yes, default argument works on django level. If you want to move it to mysql, perform ALTER TABLE ... query manually.

You also can try to extend models.IntegerField and override db_type. But it should be done for every field...

Upvotes: 2

Related Questions