类型错误:'>='不支持'str'实例和 Django 鹡鸰查询时发生的'int'错误



从django模板工作正常,但从邮差它给出这个错误。主查询如下-

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:WORKPLACEXEROTICINC HEADLESS CMSxenvlibsite-packagesdjangocorehandlersexception.py", line 47, in inner
response = get_response(request)
File "C:WORKPLACEXEROTICINC HEADLESS CMSxenvlibsite-packagesdjangocorehandlersbase.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:WORKPLACEXEROTICINC HEADLESS CMSwhlcmsvalphahomeviews.py", line 32, in blog_filter
blog_posts = blog_post_filter(selected=None, cats=categories, blog_pages=blogs, featured=filter_featured,
File "C:WORKPLACEXEROTICINC HEADLESS CMSwhlcmsvalphautilsblog_data.py", line 65, in blog_post_filter
filtered_posts = BlogPost.objects.filter(
File "C:WORKPLACEXEROTICINC HEADLESS CMSxenvlibsite-packagesdjangodbmodelsquery.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传递字符串而不是整数。

相关内容

  • 没有找到相关文章

最新更新