You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Django Admin查看djstripe同步产品/计划时报错求助

解决Django Admin查看djstripe Plan时的TypeError问题

以下是几个可行的解决方向:

  • 检查版本兼容性
    确认djstripe版本与你的Django版本、Stripe API版本匹配。部分旧版djstripe在适配Stripe新的Product/Price模型时存在bug,建议升级到最新稳定版:

    pip install --upgrade djstripe
    
  • 重新同步并校验数据
    手动触发全量同步命令,确保Stripe数据完整同步到本地:

    python manage.py djstripe_sync_models Product Price Plan
    

    同步完成后,检查数据库中djstripe_plan表,确认是否存在product_id为NULL的记录——这类无效记录会导致Admin页面渲染时触发NoneType下标错误。

  • 自定义Admin过滤无效数据
    如果存在无效的Plan记录,可通过自定义Admin类过滤掉这些数据,避免页面报错。在你的项目任意app的admin.py中添加以下代码:

    from django.contrib import admin
    from djstripe.admin import PlanAdmin as BasePlanAdmin
    from djstripe.models import Plan
    
    class FixedPlanAdmin(BasePlanAdmin):
        def get_queryset(self, request):
            # 过滤关联Product为空的无效Plan
            return super().get_queryset(request).filter(product__isnull=False)
    
    # 替换默认的PlanAdmin
    admin.site.unregister(Plan)
    admin.site.register(Plan, FixedPlanAdmin)
    
  • 清理无效数据库记录
    若确认存在损坏的Plan记录,可直接删除:

    # 在Django shell中执行
    python manage.py shell
    >>> from djstripe.models import Plan
    >>> Plan.objects.filter(product__isnull=True).delete()
    

    删除后重新同步数据即可。

  • 检查同步日志
    查看Django的日志文件,确认djstripe同步过程中是否有API报错或字段缺失的提示——这能帮你定位具体是哪条Stripe数据导致的问题。

内容的提问来源于stack exchange,提问作者Abed Paulus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 04:55:27