Django migrations detect the same change many times -


i have model this:

mymodel(models.model):     ...     date_start = models.datetimefield(         auto_now=true,         editable=true     )      date_end = models.datetimefield(         default=datetime.now() + relativedelta(months=3)     )  ... 

i modified date_end field before, , did migrations, working properly, still detecting change new migration. idea? in advance.

the problem calling datetime.now() in default parameter of definition. means default changes each time start django, system thinks need new migration.

you should wrap calculation lambda function, called necessary:

date_end = models.datetimefield(     default=lambda: datetime.now() + relativedelta(months=3) ) 

edit

if lambda causes problems, can move code separate function , pass default instead:

def default_end():     return datetime.now() + relativedelta(months=3)  ...  date_end = models.datetimefield(         default=default_end ) 

note here we're passing function object, not result, default param.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -