I would like to add a new parent class to an existing model, which inherits from the same "super" parent class. Example of initial condition:
from django.db import models
class Ticket(PolymorphicModel):
name = models.CharField(max_length=50)
company = models.CharField(max_length=80)
price = models.CharField(max_length=10)
class MovieTicket(Ticket):
# functions and logic
For my implementation, I would like to add an "intermediary" EntertainmentTicket model, which inherits from Ticket and is inherited by MovieTicket for logic grouping purposes. Desired final condition:
class Ticket(PolymorphicModel):
name = models.CharField(max_length=50)
address = models.CharField(max_length=80)
price = models.CharField(max_length=10)
class EntertainmentTicket(Ticket):
# some functions and common logic extracted
class MovieTicket(EntertainmentTicket):
# logic unique to MovieTicket
Note that the child classes have the same fields as Ticket, they only contain functions and logic. I cannot make Ticket into abstract, because I have other models pointing to it in a foreign key relationship. I use django-polymorphic to return the appropriate ticket type.
I have made migrations for EntertainmentTicket, I presume the next step is to create instances of EntertainmentTicket that point to the same Ticket instances that the current MovieTickets are pointing to. What is the best way to do this?
As long as you aren't adding extra fields to these two models, you can make them into
proxyones.This has no effect on the database and it's just how you interact with them in Django itself
Edit
I completely missed that PolymorphicModel and wasn't aware of it actually making database tables..
Proxy model's wouldn't really fit your existing scheme, so this is what I would do:
1. Rename
MovieTickettoEntertainmentTicket2. Create a new Model
MovieTicketThis could be done in two migrations, but I did
makemigrationsafter each step and then copied them into 1 & deleted #2Example End Migration:
3. Handle old
MovieTicketobjects (nowEntertainmentTicket)I did all this in a test project and it all worked, plus it keeps your
PolymorphicModelthingEdit 2
ya just had to make it more complicated, didn't you?!- this was horrible! but i did learn alot.
I recommend making a temp project and playing around with it before trying anything in Production.
Starting Models:
Starting Data:
Updated Models:
Migrations
See: Moving a Django Foreign Key to another model while preserving data? for the blueprint of how
You'll need to change the
childin all of theapps.get_model('child', 'Ticket')calls to match your appMigration #1. Temp Model, Deleting.
Steps:
marvelbecause we must deletemovieMigration #2. Reforming
Steps:
Run
python manage migrateSide Notes:
I know this is a drastic change and a lot of stuff, but holy it gets complicated quick!
I tried for the longest time to keep the original models and just repoint or rename, but because they were
One-to-OneandPrimary Keysit was doomed from the start.EntertainmentTicketconnection.I also tried doing it in a single migration, but Django didn't like the immediate recreation of exact models. it was almost as if they weren't forgotten yet.
If you had another
middlelike model like:All you'd have to do is filter out those items when creating the
EntertainmentTicketobjects in the migrations