[ Unable to set default values in Django models ]
I am trying to set default values for few fields like Date, Integer and CharFields, but when I am setting it like default=12
(in case of integer) it is not being reflected in page. However a default value of zero is always displayed.
Months = models.IntegerField('Enter the month',default=12
Above field is a required field, hence a default 0 is displayed in form but anything other than zero is not displayed.
Same is the case with charField and DateField, In this case nothing at all is displayed in form.
I am using mySQL at backend.
I want to set default date to '0000-00-00'
, How can I do that?
Answer 1
Set a default value into model
This default value is added as you are suggesting
Months = models.IntegerField('Enter the month',default=12...
But this value will be used only when a object is created and you dont receive any value to this field
Example:
class Year (models.Model):
name = models.CharField(max_length=75)
months = models.IntegerField(default=12)
Now in console/view you can do:
new_year = Year(name='any_name')
new_year.save() # And the Year object is created with months = 12
Answer 2
You should check if the default value is actually there. If you have added the default value after you created the model and applied a syncdb, make sure you run makemigrate (on Django 1.7) or an external app like South to alter the database table. If you don't or if you only run syncdb, this change won't be reflected on your MySQL.