Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8269217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:08:57+00:00 2026-06-08T06:08:57+00:00

I have the following models (I left out def __unicode__(…) for clarity): class Person(models.Model):

  • 0

I have the following models (I left out def __unicode__(...) for clarity):

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 view method for using a MinorResident object to create an OtherPerson object, like:

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

This all works great, but I still have a record in the minoresident table pointing to the person record with person_ptr_id. I also have a pointer record in the otherperson table with the same person_ptr_id pointing to the same person, and displaying all of the data as it was before the switch, but with an OtherPerson object instead of MinorResident object. So, I want to delete the MinorResident object, without deleting the parent class Person object. 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 like to not have a new record in the Person table if I can help it, since it really isn’t a new person, just a person whose an adult now. Maybe I can I do something like this? 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-keeping-parent, but I was hoping for an improved answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:08:59+00:00Added an answer on June 8, 2026 at 6:08 am

    Option 1

    Explicitly specify your parent_link fields and use an unmanaged model.

    class MinorResident(Person):
        person = models.OneToOneField(
            Person,
            parent_link = True,
            primary_key = True,
            db_column = 'person_id'
        )
        move_in_date = models.DateField(null=True)
        move_out_date = models.DateField(null=True)
        natural_child = models.NullBooleanField()
    
    
    class UnmanagedMinorResident(models.Model):
        person = models.OneToOneField(
            Person,
            primary_key = True,
            db_column = 'person_id'
        )
        move_in_date = models.DateField(null=True)
        move_out_date = models.DateField(null=True)
        natural_child = models.NullBooleanField()
    
        class Meta:
            managed = False
            db_table = MinorResident._meta.db_table
    

    Now you can call UnmanagedMinorResident.delete() without deleting the parent row.

    Option #2

    Use a raw SQL query

    from django.db import connection
    
    minor = # MinorResident object
    c = connection.cursor()
    table = MinorResident._meta.db_table
    column = MinorResident._meta.pk.column
    # In this specific case it is safe to not escape.
    sql = "DELETE FROM {0} WHERE {1}={2}".format(table, column, minor.pk)
    c.execute(sql)
    

    But you should probably change your data model and use the same table for both adults and minors. The properties you are storing in the MinorResident model do not belong there, they belong on the relationship between the MinorResident and the entity it is moving in/out from/to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =
I have the following models: class City(models.Model): name = models.CharField(max_length=100) class Pizza(models.Model): name =
Say I have the following models: class Image(models.Model): image = models.ImageField(max_length=200, upload_to=file_home) content_type =
I have following models setup in my Django application class School(models.Model): name = models.TextField()
Lets say we have following models. class User(db.Model): username=db.StringProperty() avatar=db.ReferenceProperty() class User(db.Model): username=db.StringProperty() avatar=db.StringProperty()
I have the following models: class ProjectUser(models.Model): categories = models.ManyToManyField('UserCategory', blank=True, null=True) user_id =
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have a charfield with the following: myString = models.CharField(max_length=50,null=True,blank=True) In many of my
Given the following models (cut down for understanding): class Venue(models.Model): name = models.CharField(unique=True) class
I am using Rails 3.2. I have following models: Blog Comment User class Blog

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.