I am about to update approx 2-3k of records for one of my django
model.
I know that using the update()
would help but then because I am actually using the same value in the field and change it into something else for example having a function to scrumble the existing field.
I figured there is something called F()
would should be able to help me out with it but somehow, it keeps saving my values as 0 into the field.
I did not get any error and have no idea where to look into.
this is the tutorial I read https://docs.djangoproject.com/en/1.8/ref/models/expressions/
this is my example code before I really do such batch update for the 2-3k records
# I used the below, gives me 0 when I look at my db
querysets_of_model = Model.objects.filter(id__in[111, 123]).update(name=F('name') + ' testing')
# this gives me 0 too
querysets = Model.objects.filter(id__in[111,123])
querysets.update(name=F('name') + ' testing')
# without using the F(), which of course gives me `testing only` in the name field
qs = Model.objects.filter(id__in[111, 123]).update(name='testing only')
what I originally wanted to do is something like this
qys = Model.objects.filter().update(name=scrumble_fun(F('name')))
the scrumble_fun
is just a function that scrumble the existing value then return the srcumbled values and save it.
But of course before doing this, I did as the first two example to just take out two queryets and do testing to make sure I am doing it right. But seems like I am not? :(
Thanks in advance for any help in advance.
P.S. Looping through each queryset then save()
is not the way I want to do it if that is possible.