I ran into a strange issue today when trying to use a lambda function for the default value of a ForeignKey.
my original code was this, and I expected it would look for RestaurantType instances whenever I would try to create a Restaurant:
type_of_restaurant = models.ForeignKey(RestaurantType, default = lambda: RestaurantType.objects.all()[0])
but to my surprise it would run the query when the models where loaded!
I changed to this and it behaved as I expected:
type_of_restaurant = models.ForeignKey(RestaurantType, default = lambda: RestaurantType.objects.get(id = 1))
Anyone could explain me why ? Is there something counter-intuitive with the evaluation order of the brackets or something ?
Regards,
Philippe