Reputation: 142
I am trying to save an array of text containing category types for a hotel system which looks something like this ['category-1', category-2', category-3, category-4]
. I am using category_type = ArrayField(models.CharField(max_length=200),null=True)
in my models.py
The error i get is
malformed array literal: "" LINE 1: ..., '{category-1, category-2}'::varchar(200)[], ''::varcha... ^ DETAIL: Array value must start with "{" or dimension information.
The error persist even after processing python list from ['category-1', category-2', category-3, category-4]
to {category-1, category-2, category-3, category-4}
.
I have gone through postgresql documentation and have found very limited help,
https://pganalyze.com/docs/log-insights/app-errors/U114 this is something similar posted to what i am facing problem with.
Could someone please tell me what am i doing wrong? Any help would be appreciated.
EDIT: Following is in my View.py
hotel_category=categoryTable(category_type=categorytype)
hotel_category.save()
and i am using categorytype=request.POST.getlist('category-type')
in my Views.py to get it from the POST request after user submits the form. This returns a Python list that i have mentioned above, i have manipulated this list to match PostgreSQL ArrayField with '{','}'
but i still have this error. If there is anything else you would like me to add, please let me know. :)
Upvotes: 0
Views: 517
Reputation: 142
This is an update/answer to my question for anyone who faces this issue in the future. After struggling to find help from different resources, i decided to use JSON string to store my python list.
I am using :
categorytype = json.dumps(request.POST.getlist('category-type'))
to encode and using JSONDecoder()
to fetch from database and decode. I have no idea how would this impact my further development but for now it seems a decent approach since personally i think ArrayFields are not well supported and documented in Django.
I will keep this post updated as i progress further on how this approach has impacted my development.
Have a nice day.
Upvotes: 1