Django model inheritance: Create a subclass using existing super class

I'm using multi-table-inheritance, and want to know how to create an inherited type from an instance of the superclass.

Using the example given in the documentation:

class Place(models.Model):
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=80)

class Restaurant(Place):
    serves_hot_dogs = models.BooleanField()
    serves_pizza = models.BooleanField()

Now when you create a Restaurant, you automatically make a Place, which is fine, and the behaviour I expect and want.

But what if I make a Place, and later decide I want to convert to a specific type (like Restaurant). How do you create a Restaurant, using an existing Place?

Answers


Multi-table inheritance is just OneToOneField relation between Place and Restaurant.

place = Place.objects.get(id=1)               

# Create a restaurant using existing Place   
restaurant = Resturant(place_ptr=place)
restaurant.save()

place = Place.objects.get(id=1)

# Create a restaurant using existing Place
place.__class__ = Restaurant
place.save()
restaurant = place

Need Your Help

How to get a background image to print using css?

html css printing image

I am using the ASP Net Sprites package to create CSS Sprites on my website.

Keras: How to use predict_generator with ImageDataGenerator?

python machine-learning keras deep-learning generator

I'm very new to Keras. I trained a model and would like to predict some images stored in subfolders (like for training). For testing, I want to predict 2 images from 7 classes (subfolders). The