﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
22741	Migrate fails with `TypeError` when using a callable default for a `ForeignKey`	Rik	nobody	"When using a lambda function as a default value for a ForeignKey field, `makemigrations` will fail with the exception:
{{{
    ValueError: Cannot serialize function: lambda
}}}
This is the code:
{{{
    from django.db import models


    class House(models.Model):
        address = models.CharField(max_length=100)


    class Person(models.Model):
        house = models.ForeignKey(House, default=lambda: House.objects.all()[0])
        name = models.CharField(max_length=100)
}}}
I tried to change the lambda to a defined function like this:
{{{
    from django.db import models


    class House(models.Model):
        address = models.CharField(max_length=100)


    def first_house():
        House.objects.all()[0]


    class Person(models.Model):
        house = models.ForeignKey(House, default=first_house)
        name = models.CharField(max_length=100)
}}}
In this case, `makemigrations` works, but `migrate` will now fail, with the exception:
{{{
    TypeError: int() argument must be a string or a number, not 'House'
}}}
I'm testing this in Python 3 btw."	Bug	closed	Database layer (models, ORM)	dev	Normal	needsinfo	migrations foreignkey	Simon Charette	Unreviewed	0	0	0	0	0	0
