Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bulk update of a model field based on a condition

I have a 5 column model with around 10,000+ lines of data. Now I want to update one column of all the lines based on other column values.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

myModel = MyModel.objects.all()

for l in myModel:
    if c1=='ChkVal1' and c2 < c3:    
        c4='Value 1'
    elif c1=='ChkVal2' and c2 b> c3:
        c4='Value 2'
    else
        c4='Value 3'

MyModel.objects.bulk_update(myModel, update_fields = ['c4'])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading