处理 django 模型对象的最佳方法是什么?
我需要对如何处理 django 模型上的保存功能进行一些清晰的思考。正如您将在下面看到的,我不知道如何优雅地做到这一点。我想将整个对象传递给另一个模块并在那里进行分析。我进行分析而不是在 models.py 中进行分析的动机是我将经常改进分析部分。谢谢。
models.py
from test_app.spam_analyzer import spam_checker
class SharedLink(models.Model):
link = models.URLField()
submitters_ip = models.IPAddressField()
flag = models.CharField()
def save(self, *args, **kwargs):
self.flag = spam_checker(self)
super(SharedLink, self).save(*args, **kwargs)
spam_analyzer.py
from test_app.models import SharedLink #this causes a "field not found" error
def spam_checker(SharedLink)
submitters_ip = SharedLink.submitters_ip
# see if this user's ip appears on a spam list
# if it does flag it 'spam'
return flag
I need some clear thinking on how to handle a save function on a django model. As you'll see below I am at a loss on how to do this elegantly. I'd like to pass the entire object to another module and analyze it there. My motivation for having the analysis broken out and not in models.py is that I will be frequently refining the analysis bit. Thanks.
models.py
from test_app.spam_analyzer import spam_checker
class SharedLink(models.Model):
link = models.URLField()
submitters_ip = models.IPAddressField()
flag = models.CharField()
def save(self, *args, **kwargs):
self.flag = spam_checker(self)
super(SharedLink, self).save(*args, **kwargs)
spam_analyzer.py
from test_app.models import SharedLink #this causes a "field not found" error
def spam_checker(SharedLink)
submitters_ip = SharedLink.submitters_ip
# see if this user's ip appears on a spam list
# if it does flag it 'spam'
return flag
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的错误是由循环导入引起的 - 即
models.py
导入spam_analyzer.py
,反之亦然,所以 Python 陷入了混乱。但是,使用您发布的代码,无需在spam_analyzer
中导入SharedLink
,因为您将其作为参数传递(尽管请注意,您正在传递一个实例) ,而不是类本身,因此为了清楚起见,您应该真正调用参数shared_link
)。另一种方法是使
spam_checker
函数成为 SharedLink 类的方法,这样您只需在 save() 方法中调用self.spam_checker()
即可。I presume your error is caused by a circular import - that is,
models.py
importsspam_analyzer.py
, and vice versa, so Python gets into a muddle. However with the code you've posted, there's no need to importSharedLink
inspam_analyzer
, since you pass it in as a parameter (although note that you're passing an instance, not the class itself, so you should really call the parametershared_link
for the sake of clarity).An alternative would be to make the
spam_checker
function a method of the SharedLink class, so you could just callself.spam_checker()
in your save() method.在表单层检查一下。当您第一次看到共享链接时,您可以从表单中的 clean() 例程中调用 spam_checker ;然后将标志通过链接传递给模型。
但考虑到您的垃圾邮件检查器正在不断改进,将垃圾邮件检查器实现为模型上的 flag() 方法可能会更好。因此,该标志将始终使用您最新的 spam_checking 算法。
Check it at the form layer. You might call spam_checker from the clean() routines in your form when you first sight the sharedLink; then pass the flag through to the model with the link.
But possibly better, given that your spamchecker is under constant improvement, implement spamchecker as a flag() method on the model. Thus the flag will always use your most recent spam_checking algorithm.