python - Adding an object attribute value to an array Django -
i new django , first project using it. have run issue don't understand , (perhaps i'm not using right search terms) i'm not finding relevant results in searching solutions.
i trying array of distinct entries in 'topic' field of model. playing in shell, trying figure out when got following result didn't expect.
this entered in shell:
>>> pomodoro.models import chunk, result >>> chunk.objects.all() <queryset [<chunk: onboard state estimation>, <chunk: newton's 2nd law of motion>, <chunk: if>]> >>> = [] >>> q in chunk.objects.all(): += q.topic ... >>> ['r', 'o', 'b', 'o', 't', 'i', 'c', 's', 'm', 'e', 'c', 'h', 'a', 'n', 'i', 'c', 's', 'l', 'o', 'g', 'i', 'c'] >>> ose = chunk.objects.get(pk=1) >>> ose.topic 'robotics'
i don't understand why ended array of individual letters rather array of 'topic' strings. can explain why happened?
i'm aware there better ways of doing i'm trying but, beginner, haven't got grips django properly.
try using append method instead of a += q.topic
a.append(q.topic)
or:
a += [q.topic]
the reason string objects iterable , python iterate on append items list. iterating on 'robotics'
gives ['r', 'o', 'b', 'o', 't', 'i', 'c', 's']
.
hence if specify single list value [q.topic]
python append value of q.topic
each iteration in for
loop.
Comments
Post a Comment