I have models that look like this (simplified of course):
class Product(models.Model):
product_code = Charfield(max_length=30)
class Change(models.Model):
product = ForeignKey(Product)
My problem is that this won’t work, because Products get deleted, but I need the reference in Change to remain (using on.delete to set it to null is not an option). So I have switched it to:
class Change(models.Model):
product = CharField(max_length=30)
But now I have lost the benefits of a Foreign Key field when querying. When I query for a change, I cannot select_related for Products or do any other join type action without digging into extra type clauses.
So my question, is there any good way to have my cake and eat it too? To be able to join on the field easily, but not have it be an FK? Seems like there must be some way besides FK type to say that fields are related. Perhaps through a manager?
I have this exact problem at hand… regrettably its priory was bumped pretty far down my list. However, I believe I have actually used a many-to-many field to retain a link in a different part of my application. Using the many-to-many field seems to address all of the issues I had come across. See my simplified models below.
With this setup I am able to:
Card Model
Through Model
Card Test Model