我需要一些关于如何处理 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