TypeError: '>=' not supported between instances of 'str' and 'int' error happening while Django wagtail querying

TypeError: '>=' not supported between instances of 'str' and 'int' error happening while Django wagtail querying

在 django 模板中工作正常,但在 postman 中出现此错误。主要查询如下 -

query = reduce(
                    or_, (
                        Q(path__startswith=page.path) & Q(depth=page.depth + 1) & Q(primary_category__in=list_categories)
                        | Q(additional_categories__category__in=list_categories)
                        for page in BlogPage.objects.filter(pk__in=blog_ids)
                    )
                )

这是错误的完整堆栈跟踪 -

Traceback (most recent call last):
  File "C:\WORKPLACE\XEROTICINC HEADLESS CMS\xenv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\WORKPLACE\XEROTICINC HEADLESS CMS\xenv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\WORKPLACE\XEROTICINC HEADLESS CMS\whlcmsvalpha\home\views.py", line 32, in blog_filter
    blog_posts = blog_post_filter(selected=None, cats=categories, blog_pages=blogs, featured=filter_featured,
  File "C:\WORKPLACE\XEROTICINC HEADLESS CMS\whlcmsvalpha\utils\blog_data.py", line 65, in blog_post_filter
    filtered_posts = BlogPost.objects.filter(
  File "C:\WORKPLACE\XEROTICINC HEADLESS CMS\xenv\lib\site-packages\django\db\models\query.py", line 302, in __getitem__
    (isinstance(k, slice) and (k.start is None or k.start >= 0) and
TypeError: '>=' not supported between instances of 'str' and 'int'
[04/Oct/2021 14:10:55] "GET /api/v2/blog-filter/?block_type=posts_by_category&number_of_posts=8&skip_first_posts=0&filter_featured=&blogs=4&catego
ries=2,4 HTTP/1.1" 500 13402

下面是我的函数-

def blog_post_filter(selected=None, cats=None, blog_pages=None, featured=None, posts=None, skip_first=None,
                     block_type=None):
    BlogPage = apps.get_model('blog', 'BlogPage')
    BlogPost = apps.get_model('blog', 'BlogPost')
    Category = apps.get_model('blog', 'Category')
    if block_type == 'posts_by_category':
        list_categories = []
        if cats:
            if isinstance(cats, str):
                cat_ids = cats.split(",")
                for cat in cat_ids:
                    category = Category.objects.filter(id=int(cat))
                    parents = get_parent_categories(category, Category)
                    for par in parents:
                        list_categories.append(par)
                print(f"STRING MODE: {list_categories}")
            else:
                for cat in cats:
                    category = Category.objects.filter(id=cat.value.id)
                    parents = get_parent_categories(category, Category)
                    for par in parents:
                        list_categories.append(par)
                print(list_categories)
        blog_ids = []
        if blog_pages:
            if isinstance(blog_pages, str):
                ids_blog = blog_pages.split(',')
                for b in ids_blog:
                    blog_ids.append(int(b))
                print(f"String Mode : {blog_ids}")
            else:
                for b in blog_pages:
                    blog_ids.append(b.value.id)
                print(blog_ids)
            if cats:
                query = reduce(
                    or_, (
                        Q(path__startswith=page.path) & Q(depth=page.depth + 1) & Q(primary_category__in=list_categories)
                        | Q(additional_categories__category__in=list_categories)
                        for page in BlogPage.objects.filter(pk__in=blog_ids)
                    )
                )
                print(query)
            else:
                query = reduce(
                    or_, (
                        Q(path__startswith=page.path) & Q(depth=page.depth + 1)
                        for page in BlogPage.objects.filter(pk__in=blog_ids)
                    )
                )
            if featured:
                filtered_posts = BlogPost.objects.filter(
                    query
                ).filter(
                    is_featured=True
                ).live().order_by('-first_published_at').distinct()[skip_first:(skip_first+posts)]
            else:
                filtered_posts = BlogPost.objects.filter(
                    query
                ).live().order_by('-first_published_at').distinct()[skip_first:(skip_first+posts)]
        else:
            if cats:
                if featured:
                    filtered_posts = BlogPost.objects.filter(
                        Q(primary_category__in=list_categories) |
                        Q(additional_categories__category__in=list_categories)
                    ).filter(
                        is_featured=True
                    ).live().order_by('-first_published_at').distinct()[skip_first:(skip_first+posts)]
                else:
                    filtered_posts = BlogPost.objects.filter(
                        Q(primary_category__in=list_categories) |
                        Q(additional_categories__category__in=list_categories)
                    ).live().order_by('-first_published_at').distinct()[skip_first:(skip_first+posts)]
            else:
                if featured:
                    filtered_posts = BlogPost.objects.filter(
                        is_featured=True
                    ).live().order_by('-first_published_at').distinct()[skip_first:(skip_first+posts)]
                else:
                    filtered_posts = BlogPost.objects.live().order_by(
                        '-first_published_at'
                    ).distinct()[skip_first:(skip_first+posts)]
    elif block_type == 'latest_posts':
        blog_ids = []
        if blog_pages:
            for b in blog_pages:
                blog_ids.append(b.value.id)
            query = reduce(
                or_, (
                    Q(path__startswith=page.path) & Q(depth=page.depth + 1)
                    for page in BlogPage.objects.filter(pk__in=blog_ids)
                )
            )
            if featured:
                filtered_posts = BlogPost.objects.filter(
                    query
                ).filter(
                    is_featured=False
                ).live().order_by('-first_published_at').distinct()[skip_first:(skip_first + posts)]
            else:
                filtered_posts = BlogPost.objects.filter(
                    query
                ).live().order_by('-first_published_at').distinct()[skip_first:(skip_first + posts)]
        else:
            if featured:
                filtered_posts = BlogPost.objects.filter(is_featured=False).live().order_by(
                    '-first_published_at'
                )[skip_first:(skip_first + posts)]
            else:
                filtered_posts = BlogPost.objects.live().order_by(
                    '-first_published_at'
                )[skip_first:(skip_first + posts)]
    elif block_type == 'featured_posts':
        blog_ids = []
        if blog_pages:
            for b in blog_pages:
                blog_ids.append(b.value.id)
            query = reduce(
                or_, (
                    Q(path__startswith=page.path) & Q(depth=page.depth + 1)
                    for page in BlogPage.objects.filter(pk__in=blog_ids)
                )
            )
            filtered_posts = BlogPost.objects.filter(
                query
            ).filter(
                is_featured=True
            ).live().order_by('-first_published_at').distinct()[skip_first:(skip_first + posts)]
        else:
            filtered_posts = BlogPost.objects.filter(
                is_featured=True
            ).live().order_by('-first_published_at').distinct()[skip_first:(skip_first + posts)]
    elif block_type == 'select_posts':
        post_ids = []
        for p in selected:
            post_ids.append(p.value.id)
        preserved = Case(*[When(pk=pk, then=pos) for pos, pk in enumerate(post_ids)])
        filtered_posts = BlogPost.objects.filter(pk__in=post_ids).live().order_by(preserved)
    else:
        filtered_posts = BlogPost.objects.none()
    return filtered_posts

错误跟踪显示问题发生在切片查询集时 - [skip_first:(skip_first+posts)] 部分。具体来说,skip_first 应该是一个整数,但它接收的是一个字符串。

检查调用 blog_post_filter 的代码 - 看起来您正在为 skip_first 传递字符串而不是整数。