# compare model data before save

**URL:** <https://forum.djangoproject.com/t/compare-model-data-before-save/26877>\
**Category:** Using the ORM\
**Created:** [January 12, 2024, 10:42am UTC](https://forum.djangoproject.com/t/compare-model-data-before-save/26877 "2024-01-12T10:42:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xtlc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/xtlc/32/3887_2.png) [@xtlc](https://forum.djangoproject.com/u/xtlc)\
**Post date:** [January 12, 2024, 10:42am UTC](https://forum.djangoproject.com/t/compare-model-data-before-save/26877/1 "2024-01-12T10:42:43Z")

</div>

I am not using any forms here, just data from APIs.

I have a model that has three fields:

```auto

class Example(models.Model):
    a = models.CharField ...
    b = models.ManyToManyField ...
    c = models.ForeignKeyField ...

```

I want to achieve the following:

- when a value for `a` is being saved, I want to execute `b.clear()` and `c = None` on `save()`.
- when I set `Example.b.set(...) = foo` and `Example.c = bar` I want to set `a=None` on `save()`.

so a model can either have a value in `a` or in `b` and `c` but never in both.

I tried overriding the models `save()` method, but I cannot distinguish (or can I?) between values I just loaded and the once I save. I also tried messing with `pre_save` signals, but I did not manage to come to a smart solution.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 12, 2024, 1:32pm UTC](https://forum.djangoproject.com/t/compare-model-data-before-save/26877/2 "2024-01-12T13:32:43Z")

</div>

First, there is **no** mechanism available to you to _guarantee_ this if you do not have complete control over the code being written that will update this model.

And, if you have this control, then the answer is relatively easy. Don’t allow for direct updates to a, b, and c. Create model methods for those operations and require that all updates use those methods rather than updating the model fields directly.

The issue in any situation with trying to use signals (ick) or overriding `save`, is that there are situations where those mechanisms aren’t used. (e.g., bulk\_update, or, updating the other side of the ManyToManyField).

Since you would already need to manage _those_ cases from a “coding standard” perspective, it’s not a far stretch to expand those requirements to using your model update methods.

---

<div class="post-metadata">

**Author:** ![xtlc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/xtlc/32/3887_2.png) [@xtlc](https://forum.djangoproject.com/u/xtlc)\
**Post date:** [January 13, 2024, 7:00pm UTC](https://forum.djangoproject.com/t/compare-model-data-before-save/26877/3 "2024-01-13T19:00:10Z")

</div>

I understand your input. Also I feel very uncertain when using signals.  
I built a custom save function like this:

```auto
class Example(models.Model):
    a = models.CharField ...
    b = models.ManyToManyField ...
    c = models.ForeignKeyField ...

    def safe_save(self, objects: dict):
        if "a" in objects:
            self.b.clear()
            self.c = None
            self.a= objects["a"]
        elif "c" in objects:
            self.a = None
            self.products.set(objects["products"])
            self.asset = None
        return self.save()

```
