# Update model with new object

**URL:** <https://forum.djangoproject.com/t/update-model-with-new-object/11840>\
**Category:** Using the ORM\
**Created:** [January 27, 2022, 3:21pm UTC](https://forum.djangoproject.com/t/update-model-with-new-object/11840 "2022-01-27T15:21:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tibibs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tibibs/32/1772_2.png) [@tibibs](https://forum.djangoproject.com/u/tibibs)\
**Post date:** [January 27, 2022, 3:21pm UTC](https://forum.djangoproject.com/t/update-model-with-new-object/11840/1 "2022-01-27T15:21:47Z")

</div>

Hi,

I would need some recommendations on how to handle my migration.

Indeed, previously I had an object with a bunch of properties. Let’s call it ObjectA  
Some of them need to be updated regularly and I would need to keep the history. we would then need to create a new object in a 1…n relation with ObjectA for each revision. we would end up like this:

```auto
ObjectA-----ObjectB1
        |
        ----ObjectB2
        |
        ----ObjectB3

```

This is not a problem but the tricky part comes when we would need to transfer the old properties to a new ObjectB. the next step would be to recreate a link of other objects in my model that previously were pointing to ObjectA that will now need to point to the objectB.

How could I “customize” or detail how to do my migration?  
Should I update my model incrementally?

- once to create my new objectB in my model

- a script to initialize my objectB from objectA

- update my model to create my link from different objects in my model to ObjectB

- A script to point my different object/class to objectB

- Update my model to remove my properties in ObjectA that are now covered in objectB and remove the links to objecA in other objects in my model.

I am not sure that my explanation is really clear. PLease let me know if it needs to be clarified.

Is it possible to do that in 1 step instead of all those steps?

thank you,

TIbibs

---

<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 27, 2022, 3:39pm UTC](https://forum.djangoproject.com/t/update-model-with-new-object/11840/2 "2022-01-27T15:39:30Z")

</div>

Unfortunately, your description of the situation is not clear to me.

Are you talking about keeping versioned copies of a model? If so, take a look at [Django Packages : Versioning](https://djangopackages.org/grids/g/versioning/). (If you’re looking for a package to help you do something, [djangopackages.org](http://djangopackages.org) is a great place to start your search.)

If that’s not the situation, it might be more helpful if you provided the sample models and then addressed the issue in the context of those models.

(Also, your diagram was reformatted by the forum software. You can force the forum software to maintain your spacing by enclosing the block of text between lines of three backtick - ` characters. That means you’ll have a line of ```, then your text, then another line of ```.  
This is also used when posting Python code since spaces are significant.)

---

<div class="post-metadata">

**Author:** ![tibibs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tibibs/32/1772_2.png) [@tibibs](https://forum.djangoproject.com/u/tibibs)\
**Post date:** [January 28, 2022, 5:28am UTC](https://forum.djangoproject.com/t/update-model-with-new-object/11840/3 "2022-01-28T05:28:37Z")

</div>

Hi Ken,

yes this is a bit complicated to explain out of context.  
a concrete example we could take a model like this:

_ **Product** _

- Code
- Description
- Price

this model would need to become as below:

_ **Product** _

- Code
- Description

_ **Price** _

- ProductCode
- Price
- CreationDate

This way, when the price is updated, we create a new entry in the prices object and we can keep the history of the prices.

In the example there are better ways to manage this but this is out of context to understand.  
when doing my migration, I would need to

- create a new _price_ with the price already existing in the _product_ object and a default date
- remove the _Price_ property in the _Product_ object
- Link a previous reference in other object to the _price_ to the new _price_ object instead of the _Product_ as they should get the info from there and longer directly to the product.

I wonder if I need to do some iteration and manage it in several migrations

- Migration1: Create the _Price_ Class
- Create all my prices
- Migration2: Update other objects models to link to the price
- Migration3: remove the _Price_ property in my _Product_

or if there is a way to automatize this in a single migration?

---

<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 28, 2022, 3:54pm UTC](https://forum.djangoproject.com/t/update-model-with-new-object/11840/4 "2022-01-28T15:54:36Z")

</div>

I don’t know about doing this in a single migration, but I would look at it from the other direction.

Consider this:

- Rename Product to Price. Your foreign keys will still be pointing to the right rows, you’ll just need to change the named references. (You’d be doing that anyway.)
- Create the new Product model, adding an FK relationship from (new) Price to Product.
- Iterate over all instances of Price, copying data to the new Product as appropriate and setting the FK.
- Remove the now-unnecessary fields from Price.

It’s still multiple steps, but I believe it’s less risky than trying to update all the other models that might be referring to the original Product.

I wouldn’t try to do this in one migration. I’d be looking to do this in as “bullet-proof” a method as possible.

---

<div class="post-metadata">

**Author:** ![tibibs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tibibs/32/1772_2.png) [@tibibs](https://forum.djangoproject.com/u/tibibs)\
**Post date:** [February 2, 2022, 10:35am UTC](https://forum.djangoproject.com/t/update-model-with-new-object/11840/5 "2022-02-02T10:35:57Z")

</div>

Hi,

Yes indeed much easier and safer this way.

I’ll try this way
