Home > Enterprise >  TypeError: '>=' not supported between instances of 'str' and 'int'
TypeError: '>=' not supported between instances of 'str' and 'int'

Time:10-05

Working fine from django templates but from postman it's giving this error. The main query is given below -

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)
                    )
                )

Here's the full stack trace of the error -

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

Below is my function -

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

CodePudding user response:

The error trace shows that the problem is happening when slicing the queryset - the [skip_first:(skip_first posts)] part. Specifically, skip_first should be an integer, but it's receiving a string instead.

Check the code that calls blog_post_filter - it looks like you're passing a string for skip_first instead of an integer.

  • Related