Django Silk Profiling错误

时间:2018-03-16 16:35:11

标签: python django

我正在尝试在我的开发环境中使用django-silk profiler。

页面呈现成功,但在控制台中显示以下错误,并且丝页中没有可用的分析数据。

Exception when performing meta profiling, dumping trace below
Traceback (most recent call last):
  File "site-packages/silk/middleware.py", line 122, in _process_response
    collector.finalise()
  File "site-packages/silk/collector.py", line 183, in finalise
    profile.queries = profile_query_models
  File "site-packages/django/db/models/fields/related_descriptors.py", line 509, in __set__
    % self._get_set_deprecation_msg_params(),
TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use queries.set() instead.

我正在使用python 3.6和django 2.0.3。

文章数据库模型:

class Article (models.Model):

    message            =   models.TextField ()
    user               =   models.ForeignKey ( Users,
                             on_delete=models.CASCADE, editable = False, 
                              related_name = 'articles' )

    view_count         =   models.PositiveSmallIntegerField (default = 0 )
    created_at         =   models.DateTimeField ( auto_now_add = True, editable = False )
    updated_at         =   models.DateTimeField ( auto_now = True, editable = False )


    class Meta:
        db_table                =   'articles'
        verbose_name_plural     =   "Articles"

用户模型是django默认用户模型。

view.py

class ArticleListCreateView(View):

    @silk_profile(name='Article List Profiler')
    def get (self, request, *args, **kwargs):

            ......
             # code for checking permission
             # and setting limit-offset variable
            ......
        try:            

            articles    =   Article.objects.filter(user=request.session.user).order_by('-created_at'
                            ).values('message', 'view_count')[offset:limit]

        except Exception as e:
            raise CustomException ('Server Error. Unable To Retrive Articles', 500)

        return JsonResponse( articles, safe = False )   

1 个答案:

答案 0 :(得分:0)

转到silk包的文件collector.py并从以下位置更改第183行:

profile.queries = profile_query_models

为:

profile.queries.set(profile_query_models)