#22741: Migrate fails with `TypeError` when using a callable default for a
`ForeignKey`
-------------------------------------+-------------------------------------
     Reporter:  rednaw               |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  needsinfo
     Keywords:  migrations           |             Triage Stage:
  foreignkey                         |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by timgraham:

Old description:

> 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.

New description:

 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.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/22741#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.ba8a616fa6298638f8207bc50ae04311%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to