Reputation: 512
I have a database with a model that I'm updating by adding a new field. When I added the field, all the old objects in the database populated that field with an empty string value. I need these fields to have values.
In the future, objects be forced to contain a value by either specification or a default value.
What is the best way to update this field for the old objects?
I have a couple ideas but I'm not sure if one is 'proper.' Again, this is something that only needs to be done once, as all future objects will have all required data.
UpdateModelMixin
to the ViewSet, update the fields, then disable PUT (I do not want PUT permanently allowed)Upvotes: 0
Views: 73
Reputation: 1072
I may be misunderstanding the question, but when you create a new field you can specify the default value you want to populate into the existing database so you can give it any value:
https://docs.djangoproject.com/en/2.2/ref/models/fields/#default
If you can't roll back and re-create the field, I would use the Django shell to do it since this is a one-time change, something similar to:
ModelName.objects.update(field='newvalue')
That way you don't need to change your configuration or your code or anything.
Upvotes: 1