kvi
kvi

Reputation: 15

How to use post_save with listener being method of a class

Can I use post_save with the listener being a method of a class?

What I want:

class UpdCatalog(models.Model):
    file = models.FileField(upload_to="catalog/")

class SomeClass:
    def codeType(text):
        row_code = text[0], text[1]
        return row_code, row_type

    def main(sender, instance, created, **kwargs):
        text = ["q", "w", "e". "r"]
        row_code, row_type = codeType(text)

signals.post_save.connect(SomeClass.main, sender=UpdCatalog)

When I try run this code main isn't called. There are no errors.

When the listener is not in the class everything works fine.

Upvotes: 1

Views: 181

Answers (1)

DrTyrsa
DrTyrsa

Reputation: 31971

You need to study the difference between function and instance method. And in your case you can use staticmethod decorator.

class SomeClass:
    ...

    @staticmethod
    def main(sender, instance, created, **kwargs):
        text = ["q", "w", "e". "r"]
        row_code, row_type = codeType(text)

Upvotes: 1

Related Questions