0

I need to do the 'string replace' on all my queryset, but I receive the following error:

'QuerySet' object has no attribute 'replace'

def get_profilesJson_view(self):
    queryset = Reports.objects.all().values('val_x','val_y').order_by('-time_end')[:1]
    new_queryset = queryset.replace(';', ',')
    reports_list = list(new_queryset) 
    return JsonResponse(reports_list, safe=False)

How can I do? Is it possible to use the '.filter' function? I have not experience with Django

Mixo
  • 191
  • 13
  • what is your output of `queryset`? EDIT: Shouldn't it be `queryset.replace(';', ',')`? – King Reload Mar 15 '18 at 13:28
  • @KingReload as far as I understand OP wants to udpate something in the queryset. Django's queryset does not have replace method which is reserved for strings in general. – gonczor Mar 15 '18 at 13:34
  • @gonczor https://stackoverflow.com/a/21483185/7707749 – King Reload Mar 15 '18 at 13:35
  • What? it's totally different case. – gonczor Mar 15 '18 at 13:36
  • @Mixo what is the output in your console if you write `print(queryset)`? and why do you want to replace the `;`? – King Reload Mar 15 '18 at 13:48
  • @KingReload I receive the message 'QuerySet' object has no attribute 'replace'' Because my data are separated by ';' and I need ',' – Mixo Mar 15 '18 at 14:03
  • @Mixo can you give an example of how the data looks like and how you want it? – King Reload Mar 15 '18 at 14:16
  • @KingReload I receive by jsonresponse the following obj: Object { val_x: "174.0,175.2,176.4,177.6,178.8,180.0,181.2,182.4,183.6,184.8,186.0,187.2,188.4,189.6,190.8", val_y: "0.054;0.297;0.809;1.348;1.618;1.672;2.643;2.131;2.4;2.805;7.066;35.437;35.437;5.771;0.512" } but I would send it with 'comma ' separator insted of 'semicolon' – Mixo Mar 15 '18 at 16:15

1 Answers1

2

You will need to use Func() to achieve this. You'll need something like this:

def get_profilesJson_view(self):
    queryset = Reports.objects.all().update(field_in_queryset_you_want_to_replace=Func(F('string_field'),
        Value(';'), Value(','),
        function='replace')

Compare with this answer.

gonczor
  • 3,994
  • 1
  • 21
  • 46
  • Thank you for the reply, bu it is not clear to me how to insert my fields in 'field_in_queryset_you_want_to_replace' – Mixo Mar 15 '18 at 14:00
  • Just give its name like you were passing any other keyword argument. – gonczor Mar 15 '18 at 14:00
  • I understood that: Reports.objects.all().update('val_x','val_y'=Func(F('val_x','val_y'), Value(';'), Value(','), function='replace') but I it's not correct, I receive syntax error – Mixo Mar 15 '18 at 14:50
  • `Reports.objects.all().update(val_x=Func(F('val_x'), Value(';'), Value(','), function='replace')` This should toggle commas and semicolons for `val_x` – gonczor Mar 15 '18 at 15:00
  • Thank you for your patience, the last parenthesis was missing too. Now I receive: "Argument data type text is invalid for argument 1 of replace function". However, I accepted your answer – Mixo Mar 15 '18 at 15:31
  • Is it possible to do it with more field, as asked in my first post, such as:Reports.objects.all().update(val_x,valy_y=Func(F('val_x','val_y'), Value(';'), Value(','), function='replace') unfortunately I have not solved the previous error message: "Argument data type...." – Mixo Mar 15 '18 at 16:07