python - 摆脱Python中的递归循环

标签 python django

我有以下代码,这会导致 Python“意外退出”:

def profile_videos_sort_and_filter(profile, sort, filter, uncredited_videos, list_of_credits=[]):
    """Given a sort and filter, this will order the credits and return a credit_set."""
    if filter == 'user' or filter == 'all':
        if filter == 'user':
            credit_set = profile.videocredit_set.filter(video__uploaded_by=profile)
        if filter == 'all':
            credit_set = profile.videocredit_set.all()

        if sort == 'alphabetical':
            credit_set = conform_videos_and_credits(credit_set, uncredited_videos, sort)
        if sort == 'newest':
            credit_set = conform_videos_and_credits(credit_set, uncredited_videos, sort)
        if sort =='position':
            credit_set = credit_set.order_by('position')
            list_of_credits = position_credit_set(profile, credit_set, filter, uncredited_videos)

    elif filter == 'others':
        credit_set = profile.videocredit_set.exclude(video__uploaded_by=profile)  
        if sort == 'alphabetical':
            credit_set = credit_set.order_by('video__title')
        if sort == 'newest':
            credit_set = credit_set.order_by('video__uploaded_at')
        if sort == 'position':
            credit_set = credit_set.order_by('position')
            list_of_credits = position_credit_set(profile, credit_set, filter, uncredited_videos)

    #### THIS IS THE PART THAT CAUSES THE ERROR ###
    else:
        ### if none of the above, call the function, passing default parameters ###
        profile_videos_sort_and_filter(profile, uncredited_videos=uncredited_videos,
                                        sort='position', filter='newest')

    return credit_set, list_of_credits

如何将最后的 else 子句更改为有效?谢谢。

最佳答案

sort='position' 和/或 filter='newest' 添加条件。

关于python - 摆脱Python中的递归循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7061018/

相关文章:

python - 如何在 pytest hooks 中捕获打印语句

python - 谷歌API上的凭据不足

python - 使用 modelformsets ('ManagementForm data is missing or has been tampered with' 时管理表单错误)

Python:实时渲染的最佳 3D 格式?

python - 调用 Python 对象时,模型中的 Django FileField 超出了最大递归深度

python - Python/IPython shell 中的对象字符串表示

jquery - 使用 Django 不使用 JavaScript 获取屏幕大小

python - 使用python为帖子数量添加最大限制

javascript - 将项目保存为增量 json 差异?

python - 左旋转数组