Django multi-table inheritance: update "place" to "restaurant",

The same question was already asked on SO in 2010 here , with the most recent answer being from 2014. I would like to know if this was easier with current django 2.0. I could not find anything about this in the docs.

In django docs for model inheritance, the example lists the models Placeand Restaurantas such

from django.db import models

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

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

Say I already have an object in Place, how can I raise it to Restaurant?

0
source share
1 answer

I ended up doing something like

p = Place.objects.get(name="Bob Cafe")
Restaurant.objects.create(
    place_ptr = p.id,
    serves_hot_dogs = True,
    serves_pizza = False
)
0
source

Source: https://habr.com/ru/post/1753086/


All Articles