我有一些代码遵循文档页面上给出的多表继承示例:http://docs.djangoproject.com/en/dev/topics/db/models/#multi-table-inheritance http://docs.djangoproject.com/en/dev/topics/db/models/#multi-table-inheritance。我想做的是在一个地方周围创建一家餐厅。
我已经创建了一个地方,我想在里面开一家餐厅,如下所示:
>>> p = Place.objects.get(id=12)
# If p is a Restaurant object, this will give the child class:
>>> p.restaurant
<Restaurant: ...>
>>> r = Restaurant(p)
但我刚刚收到此错误:
TypeError: int() argument must be a string or a number, not 'Place'
我想向我的模型添加更多信息,所以我不想手动将所有字段设置为相等。有办法做到这一点吗?
不幸的是,这样的“促销”模式是有问题的。正确的方法是创建一个新的Restaurant
通过复制字段来对象Place
。您必须通过迭代来完成此操作Place._meta.get_fields_with_model()
并查看这些字段属于哪个模型。我无法给你确切的代码,但如果你可以接受 GPL 代码,那么它就在transifex.txcommon.models https://github.com/transifex/transifex/blob/devel/transifex/txcommon/models.py自修订版 bcd274ce7815 起。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)