11๐
The type of each element in diseaseArray
is a np.int32
as defined by the line:
diseaseArray=np.array(diseaseArray,dtype=int) # Elements are int32
int32
cannot be serialized to JSON by the JsonResponse
being returned from the view.
To fix, convert the id value to a regular int
:
def predict(request):
...
for i in diseaseArray:
if i not in sym:
dict={'id': int(i)} # Convert the id to a regular int
dictArray.append(dict)
print(dictArray)
...
7๐
Instead of manually casting the values to ints as the accepted answer suggests, you can usually let numpy do that for you.
Instead of calling
diseaseArray=list(set(diseaseArray))
You can call
diseaseArray=diseaseArray.unique().tolist()
This should automatically convert any numpy-specific datatypes in the array to normal Python datatypes. In this case it will cast int32 to int, but it also supports other conversions.
Additionally, using numpys .unique()
might give some speedup for large datasets.
- Django 1.9 drop foreign key in migration
- Django filter by datetime on a range of dates
- Pycharm remote project with virtualenv
- Django model subclassing approaches
- Uwsgi: unrecognized option 'โmodule=MyProject.wsgi:application'
0๐
You seem to be trying to save non JSON serializable objects.
If you want to save specific objects for later use Iโd recommend you to use pickle.
https://docs.python.org/3/library/pickle.html
- Django, REST and Angular Routes
- What does it mean for an object to be unscriptable?
- Django template to populate bootstrap rows and columns
- How can I programmatically add content to a Wagtail StreamField?
- How to make FactoryBoy's ImageField generate image before save() is called?