Saturday, 17 August 2013

Select FKs unique by first occurence, append other occurences values to a field

Select FKs unique by first occurence, append other occurences values to a
field

I have this working but I'm sure sure there's a better way to accomplish
this in Django.
This is for a movie app so theres titles and people who act in each, many
to many.
I have a titlepeople model with information such as:
id, people_fk, title_fk, role_title
On movies where a cast member has alot of roles I need to display their
information like: Tom Hanks: Gardener, Police Man #1, Another Role #4
Is there anyway I can optimize the below way of doing this so the code
isn't so lengthy?
cast_unique = list()
for person in cast:
#if not in the unique list, add them
if person.people not in [p.people for p in cast_unique]:
cast_unique.append(person)
else:
# if in the list, append the role information
if person.role_title:
for c in cast_unique:
if c.people == person.people:
# append role info
c.role_title = '{0} / {1}'.format(c.role_title,
person.role_title)
Thanks in advance.

No comments:

Post a Comment