使用 Django 的 post_save() 信号

发布于 2024-10-28 08:49:48 字数 1376 浏览 0 评论 0原文

我有两个表:

class Advertisement(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    author_email = models.EmailField()

class Verification(models.Model):
    advertisement = models.ForeignKeyField(Advertisement)
    key = models.CharField(max_length=32)

添加新广告后我需要自动填充验证表。

def gen_key(sender, instance, created, **kwargs):
    if created:
        from hashlib import md5
        vkey = md5("%s%s" % (instance.author_email, instance.created_at))
        ver = Verification(advertisement=instance)
        ver.key = vkey
        ver.save()

post_save.connect(gen_key, sender=Advertisement)

当然是行不通的。姜戈1.2 问:我该怎么办?


好的,解决了一半。
问题是父模型的 post_save() 不会调用子模型。
所以你可以通过直接提供子类来解决它。

class Advertisement(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    author_email = models.EmailField()

class Sale(Advertisement):
    rooms = models.IntegerField(max_length=1)
    subway = models.ForeignKey(Subway)

class Verification(models.Model):
    advertisement = models.ForeignKeyField(Advertisement)
    key = models.CharField(max_length=32)

def gen_key(sender, instance, created, **kwargs):
    code goes here
post_save.connect(gen_key, sender=Sale, dispatch_uid="my_unique_identifier")

所以下一个问题是“如何使用父类进行 post_save() ?”

I have two tables:

class Advertisement(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    author_email = models.EmailField()

class Verification(models.Model):
    advertisement = models.ForeignKeyField(Advertisement)
    key = models.CharField(max_length=32)

And I need to auto populate Verification table after adding new advertisement.

def gen_key(sender, instance, created, **kwargs):
    if created:
        from hashlib import md5
        vkey = md5("%s%s" % (instance.author_email, instance.created_at))
        ver = Verification(advertisement=instance)
        ver.key = vkey
        ver.save()

post_save.connect(gen_key, sender=Advertisement)

Of course it doesn't work. Django 1.2
Q: How should I do it?


Ok, halfly solved.
The problem is that post_save() for parent model doesn't calling for childs models.
So you can solve it by providing child class directly.

class Advertisement(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    author_email = models.EmailField()

class Sale(Advertisement):
    rooms = models.IntegerField(max_length=1)
    subway = models.ForeignKey(Subway)

class Verification(models.Model):
    advertisement = models.ForeignKeyField(Advertisement)
    key = models.CharField(max_length=32)

def gen_key(sender, instance, created, **kwargs):
    code goes here
post_save.connect(gen_key, sender=Sale, dispatch_uid="my_unique_identifier")

So next question is "How can I use parent class for post_save()?"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

腻橙味 2024-11-04 08:49:48

无需连接到特定的发件人,只需一般连接到 post_save 并检查处理程序中已保存实例的类,例如

def gen_key(sender, **kwargs):
    if issubclass(sender, Advertisement):
        code goes here
post_save.connect(gen_key, dispatch_uid="my_unique_identifier")

instead of connecting to a specific sender, just connect to post_save in general and check the class of the saved instance in your handler eg

def gen_key(sender, **kwargs):
    if issubclass(sender, Advertisement):
        code goes here
post_save.connect(gen_key, dispatch_uid="my_unique_identifier")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文