Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.2: How to connect pre_save signal to class method

I am trying to define a "before_save" method in certain classes in my django 1.2 project. I'm having trouble connecting the signal to the class method in models.py.

class MyClass(models.Model):
    ....
    def before_save(self, sender, instance, *args, **kwargs):
        self.test_field = "It worked"

I've tried putting pre_save.connect(before_save, sender='self') in 'MyClass' itself, but nothing happens.

I've also tried putting it at the bottom of the models.py file:

pre_save.connect(MyClass.before_save, sender=MyClass)

I read about connecting signals to class methods here, but can't figure out the code.

Anybody know what I'm doing wrong?

like image 217
dmoss18 Avatar asked Jun 05 '12 20:06

dmoss18


People also ask

Where is Pre_save signal in Django?

pre_save. This is sent at the beginning of a model's save() method. Arguments sent with this signal: sender.

How do you use signals in Django?

There are 3 types of signal. pre_save/post_save: This signal works before/after the method save(). pre_delete/post_delete: This signal works before after delete a model's instance (method delete()) this signal is thrown.

Are Django signals asynchronous?

To answer directly: No. It's sync.

What is the use of the post delete signal in Django?

Django tip: To notify another part of the application after the delete event of an object happens, you can use the post_delete signal.


2 Answers

A working example with classmethod:

class MyClass(models.Model):
    #....
    @classmethod
    def before_save(cls, sender, instance, *args, **kwargs):
        instance.test_field = "It worked"

pre_save.connect(MyClass.before_save, sender=MyClass)

There's also a great decorator to handle signal connections automatically: http://djangosnippets.org/snippets/2124/

like image 177
DataGreed Avatar answered Sep 28 '22 01:09

DataGreed


I know this question is old, but I was looking for an answer to this earlier today so why not. It seems from your code that you actually wanted to use an instance method (from the self and the field assignment). DataGreed addressed how to use it for a class method, and using signals with instance methods is pretty similar.

class MyClass(models.Model)

    test_field = models.Charfield(max_length=100)

    def __init__(self, *args, **kwargs):
        super(MyClass, self).__init__(*args, **kwargs)
        pre_save.connect(self.before_save, sender=MyClass)

    def before_save(self, sender, instance, *args, **kwargs):
        self.test_field = "It worked"

I'm not sure if this is a good idea or not, but it was helpful when I needed an instance method called on an object of class A before save from class B.

like image 36
BWStearns Avatar answered Sep 28 '22 01:09

BWStearns