Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid makemigrations to re-create date field

Using Django 1.7, whenever I perform a makemigrations, I get a change on one of my models:

(venv) >> python manage.py makemigrations myapp

Migrations for 'myapp':
  0005_auto_20141206_1129.py:
    - Alter field date on observation

This is due to my Observation class using datetime.today():

class Observation(model.Models):
    date = models.DateField(default=datetime.datetime.today())

Is there an easy way to avoid creating migration files for this, but at the same time keeping the default of today() whenever an Observation is created?

like image 391
SaeX Avatar asked Dec 05 '25 17:12

SaeX


1 Answers

The problem is that each time this code is run the default changes to the current value of today(). That's because you're actually calling the function instead of passing it as a callable. Simply leave off the trailing () and you should be fine.

(Note that you should use datetime.date.today, since you want a date value, not a datetime value.)

like image 77
Kevin Christopher Henry Avatar answered Dec 08 '25 23:12

Kevin Christopher Henry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!