处理 django 模型对象的最佳方法是什么?

发布于 2024-08-05 23:55:01 字数 752 浏览 1 评论 0原文

我需要对如何处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2024-08-12 23:55:01

我认为你的错误是由循环导入引起的 - 即 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 imports spam_analyzer.py, and vice versa, so Python gets into a muddle. However with the code you've posted, there's no need to import SharedLink in spam_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 parameter shared_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 call self.spam_checker() in your save() method.

饭团 2024-08-12 23:55:01

在表单层检查一下。当您第一次看到共享链接时,您可以从表单中的 clean() 例程中调用 spam_checker ;然后将标志通过链接传递给模型。

但考虑到您的垃圾邮件检查器正在不断改进,将垃圾邮件检查器实现为模型上的 flag() 方法可能会更好。因此,该标志将始终使用您最新的 spam_checking 算法。

class SharedLink(models.Model):
    link = models.URLField()

    @property
    def flag(self):
        return spam_check(self.link)

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.

class SharedLink(models.Model):
    link = models.URLField()

    @property
    def flag(self):
        return spam_check(self.link)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文