Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法解析关键字';最后一项活动';进入野外_Python_Django_Backend - Fatal编程技术网

Python 无法解析关键字';最后一项活动';进入野外

Python 无法解析关键字';最后一项活动';进入野外,python,django,backend,Python,Django,Backend,正如title所说,我试图使用django order_by方法对帖子列表进行排序,但由于我使用的字段后来被添加到列表中(该字段不是在模型中创建的),所以失败了 除了向模型中添加字段(这是我真的不想做的),我还能做些什么吗 这是模型代码 class post(models.Model): title = models.CharField(max_length=236) content = models.TextField() post_board = models.Fo

正如title所说,我试图使用django order_by方法对帖子列表进行排序,但由于我使用的字段后来被添加到列表中(该字段不是在模型中创建的),所以失败了

除了向模型中添加字段(这是我真的不想做的),我还能做些什么吗

这是模型代码

class post(models.Model):

    title = models.CharField(max_length=236)
    content = models.TextField()
    post_board = models.ForeignKey(board, on_delete=models.CASCADE)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    release_date = models.DateField(auto_now_add=True)
    views = models.IntegerField(default=0)

    def __str__(self):
        return self.title
添加额外字段的函数:

def forumdisplay(request, boardslug=None):

    context = { 'board': None }

    if boardslug:
        context['board'] = board.objects.all().filter(slug=boardslug).first()
        
        if context['board']:
            
            context['posts'] = post.objects.all().filter(post_board=context['board'])
            
            for eachpost in context['posts']:

                eachpost.reply_count = len(reply.objects.all().filter(reply_to=eachpost))
                
                eachpost.last_activity = eachpost.release_date
                
                if eachpost.reply_count:

                    eachpost.last_activity = reply.objects.all().filter(reply_to=eachpost).order_by('release_date').first().release_date

            context['posts'] = context['posts'].order_by('last_activity')   
            alter_posts(context['posts'])
    else:
        pass

    return render(request, "board/forumdisplay.html", context)

我得到的错误是:


Request Method: GET
Request URL:    http://127.0.0.1:8000/forumdisplay/news/
Django Version: 3.0.4
Exception Type: FieldError
Exception Value:    
Cannot resolve keyword 'last_activity' into field. Choices are: author, author_id, content, id, post_board, post_board_id, release_date, reply, title, views```
你不能这样做。
您尝试使用的“按顺序”实际上将被转换为SQL命令以在数据库上执行,而数据库没有名为“最后一个活动”的列,因此您无法对其应用此函数。
将新列添加到数据库中并使其可为null有什么问题