[英]Django_comments: What's the proper way to override post_comment()?
I'm trying to customize django_comments
app ( github repo , docs ). 我正在尝试自定义
django_comments
应用程序( github repo , docs )。
The default get_form_target()
returns django_comments.views.post_comment
, which is a function-based view. 默认的
django_comments.views.post_comment
get_form_target()
返回django_comments.views.post_comment
,它是基于函数的视图。 post_comment()
, in turn, returns a HttpRedirectResponse
to the value of next
(if specified) or to a default confirmation page. post_comment()
依次将HttpRedirectResponse
返回到next
(如果已指定)的值或默认的确认页面。
See default post_comment() implementation here . 请参阅此处的默认post_comment()实现。
I need to add a couple lines of logic to the post_comment()
. 我需要在
post_comment()
添加几行逻辑。 Can I override it to where it still does the default implementation, but with the extra logic? 我是否可以将其覆盖到仍然执行默认实现的位置,但要有额外的逻辑? I could just copy the original code and modify it, but it feels like a lot of code duplication for just adding a couple lines of custom logic.
我可以复制原始代码并对其进行修改,但是感觉很像是重复了几行代码,只是添加了几行自定义逻辑。
It depends on where your logic happens relative to what is happening in the current post_comment()
. 这取决于您的逻辑相对于当前
post_comment()
发生的事情发生的位置。 If you simply want to do something before post_comment()
, then just copy a pointer to the existing func, create your func, and call the original at the end. 如果您只是想在
post_comment()
之前做某事,则只需将指针复制到现有功能,创建您的功能,并在最后调用原始功能。 Finally, you can MonkeyPatch it in by assigning your function to the other module's post_comment()
. 最后,您可以通过将函数分配给其他模块的
post_comment()
来对其进行猴子修补。
OTOH, if what you want to do is change what's happening inside the current func, then you may have to make a copy of the code, then MonkeyPatch as above. OTOH,如果您要更改当前func 内部发生的事情,则可能必须先复制代码,然后复制上面的MonkeyPatch。
Note well: even with good comments warning people about what this is doing, MonkeyPatching represents a maintenance timebomb just waiting to go off at some inconvenient time in the future. 请注意:即使有很好的评论警告人们这是在做什么,MonkeyPatching还是维护着一个炸弹,只是在将来某个不方便的时间等待启动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.