Home:ALL Converter>How to rewrite the Django model save method?

How to rewrite the Django model save method?

Ask Time:2018-01-09T15:21:41         Author:1243916142

Json Formatter

How to rewrite the Django model save method?

class Message(models.Model):
    """
    message
    """
    message_num = models.CharField(default=getMessageNum, max_length=16)  

    title = models.CharField(max_length=64)
    content = models.CharField(max_length=1024)

    def save(self, force_insert=False, force_update=False, using=None,
         update_fields=None):
        # I want send email there
        pass

I mean, in the Django model, if I create instance success, I want to call a function, such as send a email in the function.

I find in the Django model have a save method. I am not sure whether should write other code, because there are so many params.

I mean whether I only should care about my send email logic?

Author:1243916142,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48163135/how-to-rewrite-the-django-model-save-method
user2390182 :

When you override the save method, you still have to make sure that the it actually saves the instance. You can do that by simply calling the parent class' save via super:\n\nclass Message(models.Model): \n # ...\n def save(self, *args, **kwargs):\n # this will take care of the saving\n super(Message, self).save(*args, **kwargs)\n # do email stuff \n # better handle ecxeptions well or the saving might be rolled back\n\n\nYou can also connect the mail sending to the post_save (or pre_save, depending on your logic) signal. Whether you want to separate one orm the other in that way depends on how closely the two actions are linked and a bit on your taste.\n\nOverriding save gives you the option to intervene in the saving process, e.g. you can change the value of fields based on whether the mail sending was successful or not save the instance at all.",
2018-01-09T07:25:17
Marcus Lind :

The solution to what you want to do is to use Django Signals. By using Signals you can hook code to when a model is created and saved without having to rewrite the save method, that keep the separation of code and logic in a much nicer way, obviously the model does not need to know about the emails for example.\n\nAn example of how to use Signals would be to simply do the following:\n\nfrom django.db.models.signals import pre_save\nfrom django.dispatch import receiver\nfrom myapp.models import MyModel\n\n@receiver(pre_save, sender=MyModel)\ndef my_handler(sender, **kwargs):\n # Code to execute whenever MyModel is saved...\n\n\nIf you still want to override the save() method you can use the Python super() method to do so (docs).\n\nclass MyModel(models.Model):\n def save(self, *args, **kwargs):\n # This will call the parent method that you are overriding\n # so it will save your instance with the default behavior.\n super(MyModel, self).save(*args, **kwargs)\n\n # Then we add whatever extra code we want, e.g. send email...\n Messenger.send_email()\n",
2018-01-09T07:25:30
yy