-2

I'm making simple django app. I made database and I want to call it from javascript code. Everything was fine, but I had some problem with 'u' string..

models.py

class FileDB(models.Model):
  fileName = models.CharField(max_length=200)
  fileState = models.IntegerField(default=0)

views.py

from .models import FileDB

def GetFileList(request, state):
  list = FileDB.objects.filter(fileState=state).values()
  return HttpsResponse(list)

urls.py

urlpatterns = [
  ...
  url(r^getfilelist/(?P<state>[0-2]+)/$', view.GetFileList),
]

In Chrome browser : x.x.x.x:8000/getfilelist/0/

{'fileState': 0, u'id': 1, 'fileName': u'image.jpg'}{'fileState': 0, u'id': 2, 'fileName': u'image2.jpg'}{'fileState': 0, u'id': 3, 'fileName': u'picture1.jpg'}{'fileState': 0, u'id': 4, 'fileName': u'video1.avi'}

How can I remove annoying 'u' string efficiently?

6londe
  • 557
  • 1
  • 8
  • 17
  • Possible duplicate of: http://stackoverflow.com/questions/2365411/python-convert-unicode-to-ascii-without-errors – James Jul 25 '16 at 02:32
  • is `HttpsResponse` a subclass of Django's `HttpResponse`? can you show the code? I think it would be easier to change the encoding there than migrate the whole site to Python 3 :-) However if you are developing a new site it would be a good thing to go for Py 3! – mastazi Jul 25 '16 at 02:41
  • Thanks for comment. `from django.http import HttpResponse` – 6londe Jul 25 '16 at 02:49
  • I have to use Python 2.7 because 2.7 is our team's dev environment... :( – 6londe Jul 25 '16 at 02:50
  • 3
    The "u" isn't part of the data, just like the curly braces, quotes and commas aren't part of the data. Python just uses those to accurately represent what is in a list. The "u" just means that the data is a unicode string. – Bryan Oakley Jul 25 '16 at 03:24
  • Thanks! but I think it can be a problem in another language like javascript. I want to use the data in javascript.. – 6londe Jul 25 '16 at 03:44

2 Answers2

3

You can to use json library.

import json
from .models import FileDB

def get_file_list(request, state):
    list = list(FileDB.objects.filter(fileState=state).values())
    return HttpsResponse(json.dumps(list))

This should give you something like this

[{"fileState": 0, "id": 1, "fileName": "image.jpg"}, {"fileState": 0, "id": 2, "fileName": "image2.jpg"}, {"fileState": 0, "id": 3, "fileName": "picture1.jpg"}, {"fileState": 0, "id": 4, "fileName": "video1.avi"}]
Moamen
  • 706
  • 7
  • 19
0

You can remove the u characters from the keys and values of a dictionary by using a dictionary comprehension and casting the

>>> x = {'fileState': 0, u'id': 1, 'fileName': u'image.jpg'}
>>> {str(k): str(v) for k, v in x.iteritems()}
{'fileState': '0', 'id': '1', 'fileName': 'image.jpg'}

If you have a list of dictionaries, you can simply do the same but for each element in the list.

>>> y = [{'fileState': 0, u'id': 1, 'fileName': u'image.jpg'}, {'fileState': 0, u'id': 2, 'fileName': u'image2.jpg'}, {'fileState': 0, u'id': 3, 'fileName': u'picture1.jpg'}, {'fileState': 0, u'id': 4, 'fileName': u'video1.avi'}]
>>> newList = []
>>> for dct in y:
    ...     newList.append({str(k): str(v) for k, v in dct.iteritems()})
    ... 
>>> newList
[{'fileState': '0', 'id': '1', 'fileName': 'image.jpg'}, {'fileState': '0', 'id': '2', 'fileName': 'image2.jpg'}, {'fileState': '0', 'id': '3', 'fileName': 'picture1.jpg'}, {'fileState': '0', 'id': '4', 'fileName': 'video1.avi'}]
adeora
  • 557
  • 1
  • 8
  • 21