Changes between Initial Version and Version 2 of Ticket #22741


Ignore:
Timestamp:
Mar 25, 2015, 8:00:13 AM (9 years ago)
Author:
Tim Graham
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #22741

    • Property Cc Simon Charette added
    • Property Resolutionneedsinfo
    • Property Status newclosed
    • Property Summary Makemigrations fails when using lambda as default for ForeignKey fieldMigrate fails with `TypeError` when using a callable default for a `ForeignKey`
  • Ticket #22741 – Description

    initial v2  
    11When using a lambda function as a default value for a ForeignKey field, `makemigrations` will fail with the exception:
    2 
     2{{{
    33    ValueError: Cannot serialize function: lambda
    4 
     4}}}
    55This is the code:
    6 
     6{{{
    77    from django.db import models
    88
     
    1515        house = models.ForeignKey(House, default=lambda: House.objects.all()[0])
    1616        name = models.CharField(max_length=100)
    17 
     17}}}
    1818I tried to change the lambda to a defined function like this:
    19 
     19{{{
    2020    from django.db import models
    2121
     
    3232        house = models.ForeignKey(House, default=first_house)
    3333        name = models.CharField(max_length=100)
    34 
     34}}}
    3535In this case, `makemigrations` works, but `migrate` will now fail, with the exception:
    36 
     36{{{
    3737    TypeError: int() argument must be a string or a number, not 'House'
    38 
     38}}}
    3939I'm testing this in Python 3 btw.
Back to Top