I have the following models (for clarity, I left def __unicode__(...) ):
class Person(models.Model): first_name = models.CharField(max_length=64, null=True, blank=True) middle_name = models.CharField(max_length=32, null=True, blank=True) last_name = models.CharField(max_length=64, null=True, blank=True) class MinorResident(Person): move_in_date = models.DateField(null=True) move_out_date = models.DateField(null=True) natural_child = models.NullBooleanField() class OtherPerson(Person): associate_all_homes = models.BooleanField(default=False)
I have the following presentation method for using the MinorResident object to create the OtherPerson object, for example:
def MinorToAdult(request, minor): p = Person.objects.get(id=minor.person_ptr_id) o = OtherPerson(p.id) o.__dict__.update(p.__dict__) o.save() return True
All of this works fine, but I still have an entry in the minorizer table indicating a person entry with person_ptr_id. I also have a pointer entry in the otherperson table with the same person_ptr_id pointing to the same person and displaying all the data as it was before the switch, but with the OtherPerson object instead of the MinorResident object. So, I want to delete the MinorResident object without deleting the Person object of the parent class. I suppose I can do something like:
p = Person.objects.get(id=minor.person_ptr_id) o = OtherPerson() o.__dict__.update(p.__dict__) o.save() minor.delete() return True
But I would not want to have a new entry in the Person table, if I can help her, because this is really not a new person, but just a person, an adult now. Maybe I can do something like that? Or is there a better way to handle model transmutation?
p = Person.objects.get(id=minor.person_ptr_id) o = OtherPerson(p.id) o.__dict__.update(p.__dict__) o.save() minor.person_ptr_id = None minor.delete() return True
I looked at SO # 3711191: django-deleting-object-keep-parent , but I was hoping for an improved response.