django多表继承:将"place"升级为django.到“餐厅" [英] django multi-table inheritance: upgrade a "place" to a "restaurant"

查看:44
本文介绍了django多表继承:将"place"升级为django.到“餐厅"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2010年,在SO上已经问过同样的问题,此处,最近的答案是2014年.我想知道在当前的django 2.0中是否更简单.我在文档中找不到任何有关它的信息.

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 got simpler with the current django 2.0. I couldn't find anything about it in the docs.

在django文档中,用于模型继承,该示例列出了这样的 Place Restaurant 模型

In the django docs for model inheritance, the example lists a Place and Restaurant model as 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)

说我在 Place 中已经有一个对象,如何将其提升为 Restaurant ?

Say I already have an object in Place, how can it be promoted to a Restaurant?

推荐答案

我最终做了类似的事情

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

这篇关于django多表继承:将"place"升级为django.到“餐厅"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆