Quantcast
Channel: User Amir Afianian - Stack Overflow
Viewing all articles
Browse latest Browse all 39

Answer by Amir Afianian for Converting Django BooleanField to NullBooleanField and changing default

$
0
0

It's taking you so long because of this for loop:

    objects= MyModel.objects.active().filter(my_boolean=False)    for object in objects:        object.my_boolean = None        object.save()  # Database roundtrip 

Namely, for each object, you are hitting the database. The better way is to bulk_update the fields all at once:

objects= MyModel.objects.active().filter(my_boolean=False).update(my_boolean=None)

You can perform the query above inside your shell. It's better not to include it in your migration files for it may be executed each time you run migrations.

Enter you Django shell by python manage.py shell, import your model, and execute the query above.


Viewing all articles
Browse latest Browse all 39

Trending Articles





<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>