'Post' 和 'Post' django 实例之间不支持“<”
'<' not supported between instances of 'Post' and 'Post' django
当我尝试从列表 pposts 中循环发布帖子时
我得到这个错误
**'<' not supported between instances of 'Post' and 'Post'**
TypeError at /
'<' not supported between instances of 'Post' and 'Post'
TypeError at /
'<' not supported between instances of 'Post' and 'Post'
TypeError at /
'<' not supported between instances of 'Post' and 'Post'
TypeError at /
'<' not supported between instances of 'Post' and 'Post'
models.py
class Post(models.Model):
ch = [
('pvp','pvp'),
('Default','Default'),
('bedrock','bedrock')
]
xch = [
('x1','x1'),
('x4','x4'),
('x8','x8'),
('x16','x16'),
('x32','x32'),
('x64','x64'),
('x128','x128'),
('x256','x256'),
('x512','x512'),
]
vch = [
('1.7','1.7'),
('1.8','1.8'),
('1.9','1.9'),
('1.10','1.10'),
('1.11','1.11'),
('1.12','1.12'),
('1.13','1.13'),
('1.14','1.14'),
('1.15','1.15'),
('1.16','1.16'),
('1.17','1.17'),
]
author = models.ForeignKey(Profile,on_delete=models.CASCADE)
title = models.CharField(max_length=150)
slug = models.SlugField(null=True, editable=False)
content = models.TextField()
mainimage = models.ImageField(null=True ,upload_to='photes')
image1 = models.ImageField(null=True, upload_to='inphotes')
image2 = models.ImageField(null=True, upload_to='inphotes')
image3 = models.ImageField(null=True, upload_to='inphotes')
file = models.FileField(null=True, upload_to='files')
version = models.CharField(max_length=50, null=True)
xnumber = models.CharField(max_length=50, choices=xch, null=True)
category = models.CharField(max_length=50, choices=ch, null=True)
discord_id = models.CharField(max_length=150)
date_added = models.DateTimeField(auto_now_add=True)
likes = models.ManyToManyField(User, related_name='blog_posts')
def total_likes(self):
return self.likes.count()
def __str__(self):
return self.title + str(self.author)
def save(self, *args, **kawrgs):
slug = slugify(self.title)
_2 = 'post'
has_slug = Post.objects.filter(slug=slug).exists()
count = 1
while has_slug:
count += 1
slug = slugify(self.title) + '-' + str(count)
has_slug = Post.objects.filter(slug=slug).exists()
self.slug = slug
super().save(*args, **kawrgs)
class Meta:
ordering = ['-date_added']
def get_absolute_url (self):
return reverse('post_detail', kwargs={'pk': str(self.pk)})
views.py
def frontpage(request, *args, **kwargs):
#loged in profile
profile = Profile.objects.get(user= request.user)
#check who we are following
users = [user for user in profile.following.all()]
#val
pposts = []
qs = None
#get the posts pepole we follow
for u in users:
p= Profile.objects.get(user=u)
p_posts = p.post_set.all()
pposts.append(p_posts)
#my posts
mposts = []
my_posts = profile.profiles_posts()
mposts.append(my_posts)
if len(pposts) > 0:
qs = sorted(chain(*pposts), reverse=True)
return render(request, 'posts/frontpage.html', {'posts': qs, 'pposts': qs })
我不知道如何解决
问题不在循环上,而在 sorted()
函数上。此函数需要一种方法来比较可迭代对象的两个对象。默认情况下,它使用 <
运算符,如果没有为您的对象实现,它会抛出错误。你必须像这样使用 sorted
:
sorted(ppost, key=aFunctionThatTakesTwoObjectsAndReturnABoolean)
当我尝试从列表 pposts 中循环发布帖子时 我得到这个错误
**'<' not supported between instances of 'Post' and 'Post'**
TypeError at /
'<' not supported between instances of 'Post' and 'Post'
TypeError at /
'<' not supported between instances of 'Post' and 'Post'
TypeError at /
'<' not supported between instances of 'Post' and 'Post'
TypeError at /
'<' not supported between instances of 'Post' and 'Post'
models.py
class Post(models.Model):
ch = [
('pvp','pvp'),
('Default','Default'),
('bedrock','bedrock')
]
xch = [
('x1','x1'),
('x4','x4'),
('x8','x8'),
('x16','x16'),
('x32','x32'),
('x64','x64'),
('x128','x128'),
('x256','x256'),
('x512','x512'),
]
vch = [
('1.7','1.7'),
('1.8','1.8'),
('1.9','1.9'),
('1.10','1.10'),
('1.11','1.11'),
('1.12','1.12'),
('1.13','1.13'),
('1.14','1.14'),
('1.15','1.15'),
('1.16','1.16'),
('1.17','1.17'),
]
author = models.ForeignKey(Profile,on_delete=models.CASCADE)
title = models.CharField(max_length=150)
slug = models.SlugField(null=True, editable=False)
content = models.TextField()
mainimage = models.ImageField(null=True ,upload_to='photes')
image1 = models.ImageField(null=True, upload_to='inphotes')
image2 = models.ImageField(null=True, upload_to='inphotes')
image3 = models.ImageField(null=True, upload_to='inphotes')
file = models.FileField(null=True, upload_to='files')
version = models.CharField(max_length=50, null=True)
xnumber = models.CharField(max_length=50, choices=xch, null=True)
category = models.CharField(max_length=50, choices=ch, null=True)
discord_id = models.CharField(max_length=150)
date_added = models.DateTimeField(auto_now_add=True)
likes = models.ManyToManyField(User, related_name='blog_posts')
def total_likes(self):
return self.likes.count()
def __str__(self):
return self.title + str(self.author)
def save(self, *args, **kawrgs):
slug = slugify(self.title)
_2 = 'post'
has_slug = Post.objects.filter(slug=slug).exists()
count = 1
while has_slug:
count += 1
slug = slugify(self.title) + '-' + str(count)
has_slug = Post.objects.filter(slug=slug).exists()
self.slug = slug
super().save(*args, **kawrgs)
class Meta:
ordering = ['-date_added']
def get_absolute_url (self):
return reverse('post_detail', kwargs={'pk': str(self.pk)})
views.py
def frontpage(request, *args, **kwargs):
#loged in profile
profile = Profile.objects.get(user= request.user)
#check who we are following
users = [user for user in profile.following.all()]
#val
pposts = []
qs = None
#get the posts pepole we follow
for u in users:
p= Profile.objects.get(user=u)
p_posts = p.post_set.all()
pposts.append(p_posts)
#my posts
mposts = []
my_posts = profile.profiles_posts()
mposts.append(my_posts)
if len(pposts) > 0:
qs = sorted(chain(*pposts), reverse=True)
return render(request, 'posts/frontpage.html', {'posts': qs, 'pposts': qs })
我不知道如何解决
问题不在循环上,而在 sorted()
函数上。此函数需要一种方法来比较可迭代对象的两个对象。默认情况下,它使用 <
运算符,如果没有为您的对象实现,它会抛出错误。你必须像这样使用 sorted
:
sorted(ppost, key=aFunctionThatTakesTwoObjectsAndReturnABoolean)