0

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.

Dora
  • 6,776
  • 14
  • 51
  • 99
  • 1
    This might help you: https://stackoverflow.com/questions/3300944/can-i-use-django-f-objects-with-string-concatenation – T.Tokic May 20 '18 at 11:58
  • @T.Tokic what I want is actually trying to take out the original value, do something with it in a function, then save it back to the original field though. Instead of just `concating` – Dora May 20 '18 at 18:42

1 Answers1

1

Does this work?

querysets = Model.objects.filter(id__in[111, 123])
querysets.name = F('name') + ' testing'
querysets.save()
Andrey Shipilov
  • 1,986
  • 12
  • 14
  • gives me `'QuerySet' object has no attribute 'save'` – Dora May 19 '18 at 22:57
  • @Dora oh. Just run through them in a loop and save each. – Andrey Shipilov May 19 '18 at 23:06
  • I should have noted that's not the way I want to do if I don't have to :| Let me re-edit my question – Dora May 19 '18 at 23:09
  • Try this. `from django.db.models import Value; from django.db.models.functions import Concat; querysets.update(name=Concat('name', Value('test')))` – Andrey Shipilov May 19 '18 at 23:32
  • no luck :( `p.update(name=Concat('name', Value(' does this work')))` actually did not even change a thing. No errors too – Dora May 20 '18 at 16:52
  • also, as mentioned. I am trying to take out the original value, do something with it in a function, then save it back to the original field though – Dora May 20 '18 at 18:38
  • that was my bad, actually it worked. I was looking at the wrong field, but then something else about `F()` came up that I cannot figure out. I will have another post open Thanks a lot – Dora May 20 '18 at 20:51