Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.7.1 Makemigrations fails when using lambda as default for attribute

I'm using Django 1.7.1. My model looks like this:

from datetime import datetime
from django.db import models

class myModel(models.Model):
    x = models.CharField(max_length=254,null=True, blank=True,)

Everything works perfectly fine.

However, when I add the following attribute to myModel, it breaks:

    y = models.DateTimeField(default=lambda: datetime.utcnow() + timedelta(days=1), editable=False)

manage.py makemigrations gives me the following error:

ValueError: Cannot serialize function: lambda

This seems like a known bug: http://comments.gmane.org/gmane.comp.python.django.scm/125724

So how can I work around it? I need the value of y to be automatically set by default to 24 hours from the moment the model was created.

like image 941
Saqib Ali Avatar asked Nov 21 '14 23:11

Saqib Ali


People also ask

What does Makemigrations do in Django?

makemigrations is responsible for packaging up your model changes into individual migration files - analogous to commits - and migrate is responsible for applying those to your database.

What does Makemigrations command do?

makemigrations basically generates the SQL commands for preinstalled apps (which can be viewed in installed apps in settings.py) and your newly created apps' model which you add in installed apps. It does not execute those commands in your database file.

What is Makemigrations?

makemigrations simply analyzes your current models for any changes that would be out of sync with your database and creates a migrations file that can be used to bring the in sync. If left at this point, your models would still be out of sync with your database possibly breaking your code that queries the database.

How does Django keep track of migrations?

Django keeps track of applied migrations in the Django migrations table. Django migrations consist of plain Python files containing a Migration class. Django knows which changes to perform from the operations list in the Migration classes. Django compares your models to a project state it builds from the migrations.


1 Answers

The migrations documentation addresses this:

Migrations are just Python files containing the old definitions of your models - thus, to write them, Django must take the current state of your models and serialize them out into a file. While Django can serialize most things, there are some things that we just can’t serialize out into a valid Python representation....

Django can serialize the following: Any function or method reference... in module’s top-level scope

Django cannot serialize: Lambdas

So the solution is simple: instead of a lambda, define a regular function and refer to it by name.

def one_day_hence():
    return datetime.utcnow() + timezone.timedelta(days=1)

class MyModel(models.Model):
    y = models.DateTimeField(default=one_day_hence)
like image 151
Kevin Christopher Henry Avatar answered Oct 13 '22 00:10

Kevin Christopher Henry