Home > database >  How to access contents of a SimpleArrayField in Python Django
How to access contents of a SimpleArrayField in Python Django

Time:11-08

I am currently working with a form class in Django. I need to be able to access data contained in a SimpleArrayField.

class SignUps(forms.Form)
 people = SimpleArrayField(forms.EmailField(), label="name")

Let's say a user fills out the form with the names "John Smith," "Jane Smith", "Mike Smith".

I need to be able to access the names. I've tried various methods such as

print(people["name"])

or

print(people[0])

But I keep getting the error

TypeError: 'SimpleArrayField' object is not subscriptable.

I've tried to figure out to access fields from a SimpleArrayObject, but I don't see any examples in documentation. How would I go about getting the names from the people object? Is that even possible to do?

CodePudding user response:

The SimpleArrayField is intended to allow you to encode a list of values together as a single string (e.g. the value "1,2,3,4" could represent the array of integers [1,2,3,4]). You have specified that the values in the array are forms.EmailField(), so it's expecting that the value will be something like "[email protected],[email protected]". When the form is submitted, you'll have an array of email strings under the "name" key in the form.cleaned_data

  • Related