[英]How to sub-select rows for equality with float dtype using pandas
[英]Django filter error sub-select returns 2 columns - expected 1
我想在当前页面详细信息上建议相同的帖子类别所以我尝试以下代码:
我的观点:
def recipe(request, slug, message=''):
post = get_object_or_404(Post, slug=slug)
post_category = post.category.all() # get the categories of the post
posts_same_category = Post.objects.filter(
Q(category__name__icontains= post_category) # filter the same categories of the other posts
).filter(published=True).exclude(slug=slug) # exclude the current post and no publish posts
我的模型:
class PostCategory(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=250, null=True, blank=True)
category = models.ManyToManyField('PostCategory', blank=True)
mealtype = models.ManyToManyField('MealType', blank=True)
我在帖子详细信息页面上有一个错误:
sub-select returns 2 columns - expected 1
我认为问题出在 ManytoManyField 上,但我做错了什么?
在此先感谢您的帮助 !
与CharField相比,您正在传递对象。
这里,
category__name__icontains= post_category
#category__name ---> CharField
#post_category ---> Query set [<PostCategory obj>....]
尝试仅获取您的名称或相应更改查询的内容:
posts_same_category = Post.objects.filter(category__in=post.category.all(), published=True).exclude(slug=slug)
另外,你可以看看prefetch_related
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.