我有这个工作,但我敢肯定,必须有一个更好的方法Django的,过滤器和组查询结果由现场
的背景是电影/电视应用程序,以便有职称(电影/电视)和在每个人中行动的人,多对多的关系。
我有一个“titlepeople”模式的信息,如:
id, people_fk, title_fk, role_title
在电影,其中一个演员有很多我需要显示自己的信息,如角色: 汤姆·汉克斯:花匠,警察的男子# 1,另一个角色#4
是否有无论如何我可以优化下面这样做这样的代码是不是太冗长?
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)
感谢
你能粘贴在'models.py'文件中吗? –