0

I'm trying to view a python list of objects that was a part of some input in an html form:

$('input[name=my_array]').serializeObject()

where my input tag would be something like this:

<input type="hidden" name="my_array" value="{{ my_array_of_objects }}">

But I'm just getting something like this:

[<object_type: an_object object>]

So I can't access any properties of this object. Is there a way to convert this to where I can view what these are referencing to? I've been able to iterate over them in previous instances, and access their properties in my HTML file:

{% for o in my_array_of_objects %}
   <li> o.id, o.first_name, o.last_name </li>
{% endfor %}

So is there a similar way I can access this object in javascript? Sorry if this is noob question.

EDIT: Here is a sample of what I'm trying to pass. I'm using a django rest framework, where my objects are stored in a SQLDB, under python classes like so:

class my_object(models.Model):

  study_id = models.CharField(max_length = 30, blank = True)
  g1 = models.ForeignKey(G1)
  g2 = models.ForeignKey(G2)

  title = models.IntegerField(choices = TITLE_CHOICES)
  family = models.IntegerField(choices = FAMILY_CHOICES)

and when a I render some page, I create a list of these objects that I need, let's just call it my_list_of_objects. So, in the HTML form, I have an input:

<input type="hidden" name="my_list" value="{{ my_list_of_objects }}">

and when I try to reference this value on the JavaScript side after I submit my form, all I get is a list of

my_list_of_objects =[<my_object: my_object object>, <my_object: my_object object>, ...]

2 Answers 2

2

Can't you convert that object into a JSON object and access it.

JSON.parse($('input[name=my_array]').val());

I still didn't get that, how you create that list of object. (may be because I don't know much about Python)

By the way, I suggest you to convert your Python object list to a JSON object in the server page.

json.dumps(c.__dict__)

I got that line from here. Then you might able to use JSON.parse in your java script.

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

5 Comments

The value still comes up as [<object_type: an_object object>]
Can you please put the sample data for the input value?
I added it in the edit, hopefully this explains it a little better
I feel like it's getting closer? I'm getting a type error now: <django.db.models.sql.query.Query object at 0x110dd6890> is not JSON serializable
I'm afraid that I won't be much helpful for that, but have a look on this post
0

Try JSON.serialize(my_array_of_objects)

1 Comment

In the form? Or in JavaScript?

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.