Changes between Initial Version and Version 2 of Ticket #22741
- Timestamp:
- Mar 25, 2015, 8:00:13 AM (10 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #22741
- Property Cc added
- Property Resolution → needsinfo
- Property Status new → closed
- Property Summary Makemigrations fails when using lambda as default for ForeignKey field → Migrate fails with `TypeError` when using a callable default for a `ForeignKey`
-
Ticket #22741 – Description
initial v2 1 1 When using a lambda function as a default value for a ForeignKey field, `makemigrations` will fail with the exception: 2 2 {{{ 3 3 ValueError: Cannot serialize function: lambda 4 4 }}} 5 5 This is the code: 6 6 {{{ 7 7 from django.db import models 8 8 … … 15 15 house = models.ForeignKey(House, default=lambda: House.objects.all()[0]) 16 16 name = models.CharField(max_length=100) 17 17 }}} 18 18 I tried to change the lambda to a defined function like this: 19 19 {{{ 20 20 from django.db import models 21 21 … … 32 32 house = models.ForeignKey(House, default=first_house) 33 33 name = models.CharField(max_length=100) 34 34 }}} 35 35 In this case, `makemigrations` works, but `migrate` will now fail, with the exception: 36 36 {{{ 37 37 TypeError: int() argument must be a string or a number, not 'House' 38 38 }}} 39 39 I'm testing this in Python 3 btw.