Skip to content
Advertisement

Django 2.2 cannot serialize default values once migration has been done

I have a model which is refered to as a foreignkey with on_delete set to SET_DEFAULT. Because of that, I need to provide this model with a default item. I created a static method which does just that.

JavaScript

I am able to run makemigrations and migrate without issue.

My problem starts when I modifiy my models and try to use makemigrations again in order to update the migrations files. I get the error :

ValueError: Cannot serialize: <ScheduleChoice: Every minute> There are some values Django cannot serialize into migration files. For more, see https://docs.djangoproject.com/en/2.2/topics/migrations/#migration-serializing

I tried to apply this answer, but it didn’t help. Why does Django need to serialize my default value ? Why does it only do so after the first migration has successfully ended ?

I can always use reset_db and do my migration, but it is not acceptable in my production environment.

How can I fix this ?

Advertisement

Answer

Django needs to serialize your models to make migration files for them. Hence it also needs to serialize most of the attributes you set on the model fields (default included). Currently you define a method and directly call that instead of providing the method as the default, also your method returns a tuple with ScheduleChoice and a boolean.

Django can serialize booleans but not the model instance (for the migration) hence you get an error, not to mention the tuple would have caused an error anyway. You should not call the method and instead just pass the method as the default, also instead of returning the instance return only the pk, also ideally this method should simply be a function:

JavaScript
Advertisement