Cancel saving model when using pre_save in django

Alfred Huang

I have a model:

class A(models.Model):
    number = models.IntegerField()

But when I call A.save(), I want to ensure that number is a prime (or other conditions), or the save instruction should be cancelled.

So how can I cancel the save instruction in the pre_save signal receiver?

@receiver(pre_save, sender=A)
def save_only_for_prime_number(sender, instance, *args, **kwargs):
    # how can I cancel the save here?
Alfred Huang

See my another answer: https://stackoverflow.com/a/32431937/2544762

This case is normal, if we just want to prevent the save, throw an exception:

from django.db.models.signals import pre_save, post_save

@receiver(pre_save)
def pre_save_handler(sender, instance, *args, **kwargs):
    # some case
    if case_error:
        raise Exception('OMG')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django pre_save not saving changes

From Dev

when to use pre_save, save, post_save in django?

From Dev

Django - Saving a model using Admin

From Dev

Laravel Ardent not saving model when using updateUniques()

From Dev

Django Form not saving when using a custom field

From Dev

Mongoose Pre-Save Hook is Firing, but Not Saving Additional Field (NOT using model.update)

From Dev

django saving model with formset

From Dev

Django Model Form not saving

From Dev

How to add current user data when saving in django model

From Dev

Django User Model: add extra behavior when saving

From Dev

Passing arguments django signals - post_save/pre_save

From Dev

Django 1.7 - inserting model data using bulk_create and then saving

From Dev

Getting url after a saving file (not using django model)

From Dev

Using full calendar in django admin; Saving the external dragged item to a model

From Dev

Using enum in a model, when saving it doesn't like the integer value

From Dev

Using enum in a model, when saving it doesn't like the integer value

From Dev

Django pre_save signal - would an exception fail the transaction?

From Dev

Method save does not exist when saving Laravel 5.2 model

From Dev

Saving a model with a foreign key in django

From Dev

Saving a model containing a FileField in Django

From Dev

Saving a model with a foreign key in django

From Dev

Django saving in a model with User field

From Dev

Django model prevent saving to database

From Dev

Django: When saving issue

From Dev

Using a pre_save while specifying update_fields

From Dev

Resizing an image using PIL in pre_save signal

From Dev

Update automatically a specific field in Django model when saving other many-to-many related model

From Dev

Saving additional information to the database conditionally when using a modelfrom in django

From Dev

Saving django model instance into another model

Related Related

  1. 1

    Django pre_save not saving changes

  2. 2

    when to use pre_save, save, post_save in django?

  3. 3

    Django - Saving a model using Admin

  4. 4

    Laravel Ardent not saving model when using updateUniques()

  5. 5

    Django Form not saving when using a custom field

  6. 6

    Mongoose Pre-Save Hook is Firing, but Not Saving Additional Field (NOT using model.update)

  7. 7

    django saving model with formset

  8. 8

    Django Model Form not saving

  9. 9

    How to add current user data when saving in django model

  10. 10

    Django User Model: add extra behavior when saving

  11. 11

    Passing arguments django signals - post_save/pre_save

  12. 12

    Django 1.7 - inserting model data using bulk_create and then saving

  13. 13

    Getting url after a saving file (not using django model)

  14. 14

    Using full calendar in django admin; Saving the external dragged item to a model

  15. 15

    Using enum in a model, when saving it doesn't like the integer value

  16. 16

    Using enum in a model, when saving it doesn't like the integer value

  17. 17

    Django pre_save signal - would an exception fail the transaction?

  18. 18

    Method save does not exist when saving Laravel 5.2 model

  19. 19

    Saving a model with a foreign key in django

  20. 20

    Saving a model containing a FileField in Django

  21. 21

    Saving a model with a foreign key in django

  22. 22

    Django saving in a model with User field

  23. 23

    Django model prevent saving to database

  24. 24

    Django: When saving issue

  25. 25

    Using a pre_save while specifying update_fields

  26. 26

    Resizing an image using PIL in pre_save signal

  27. 27

    Update automatically a specific field in Django model when saving other many-to-many related model

  28. 28

    Saving additional information to the database conditionally when using a modelfrom in django

  29. 29

    Saving django model instance into another model

HotTag

Archive