0

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. :)

5
  • 2
    Can you include the actual code where you're assigning the values to category_type and invoking the save() method? Commented Aug 18, 2019 at 22:43
  • @FlipperPA i have Edited my question and added further details at the end :) Commented Aug 19, 2019 at 10:12
  • What version of django are you using? Commented Aug 19, 2019 at 11:31
  • @mattjegan Django version 2.2.2 Commented Aug 19, 2019 at 15:15
  • Did you guys have a look at it? Commented Aug 21, 2019 at 11:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.