Changes between Version 1 and Version 2 of Ticket #26488, comment 8


Ignore:
Timestamp:
Aug 10, 2017, 10:11:53 PM (7 years ago)
Author:
Christopher Neugebauer

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #26488, comment 8

    v1 v2  
    11So here's something fun. Here's how you *can* create a model with a base class whose name you can change, and Migrations will be happy.
    22
    3 **DO NOT ACTUALLY DO THIS IN PROD :)**
     3**EXAMPLE DELETED BECAUSE THE NEXT COMMENT SUPERSEDES IT IN A MUCH NICER WAY **
    44
    5 1. Create a model `SubModel`, it must derive from `models.Model`.
    6 2. Create a new base class, `BaseModel`, it must derive from `models.Model`
    7 3. Add: `basemodel_ptr = OneToOneField(BaseModel, null=True)` on `SubModel`. The field *must* be named like the class name of the new base class.
    8 4. Automatically create a migration
    9 5. Create a data migration that creates a new `BaseModel(id=submodel.id)` for each `SubModel` instance. This step is only necessary if you have created `SubModel` objects. Otherwise, just use an automatic default.
    10 6. Set the base model of `SubModel` to `BaseModel`
    11 7. Automatically create a migration
    12 8. Rename `BaseModel` to `RenamedBaseModel`
    13 9. Automatically create a migration, accepting the two automatic suggestions
    14 10. Migrate!
    15 
    16 At this point, you can see that `RenamedBaseModel` works as expected. It appears as though everything here works, as long as you aren't tracking the base class when migrations first creates the model.
     5You can see that `RenamedBaseModel` works as expected. It appears as though everything here works, as long as you aren't tracking the base class when migrations first creates the model.
Back to Top