[英]Django multiple model inheritance, django-push-notifications
I am writing a back end for a mobile app using Django 1.8. 我正在使用Django 1.8为移动应用程序编写后端。 The
django-push-notifications
lib provides the GCMDevice
model. django-push-notifications
库提供了GCMDevice
模型。 The problem is I already have a Device
model with some mandatory field and some logic I don't want to lose. 问题是我已经有了一个
Device
模型,其中包含一些必填字段和一些我不想丢失的逻辑。
What I wanted to do is to inherit whole GCMDevice
functionality and adjust them to my Device model(which btw inherits another mixin providing spatial data fields, with custom object Manager set which I want to keep). 我想要做的是继承整个
GCMDevice
功能并将其调整为我的设备模型(顺便说一句,它继承了另一个提供空间数据字段的mixin,并保留了我要保留的自定义对象管理器)。 I read about 3 different django model inheritance ideas but none of them seem to solve my problem (keeping managers, provide django-push-notifications
functionality, keeping my Device
model fields). 我阅读了大约3种不同的django模型继承思想,但似乎都无法解决我的问题(保留管理器,提供
django-push-notifications
功能,保留我的Device
模型字段)。 Maybe OneToOne association will do the work? 也许OneToOne协会会做这项工作吗?
IDEA: 理念:
class Device(MyMixin):
gcm_device = models.OneToOneField(GCMDevice)
my_other_field = models.TextField()
def send_message(self, payload):
self.gcm_device.send_message(payload)
Any reason why it is necessary to inherit? 有什么必要继承的原因吗? Could composition be a substitute for what you are trying to do by inheritance?
合成可以替代您通过继承尝试执行的操作吗?
----------------- Edited answer ----------------- -----------------编辑答案-----------------
class Device(models.Model):
...
gcm_device = models.OneToOneField(GCMDevice)
I have given the most simple case, you could make the GCM device your primary key as well. 我给出了最简单的情况,您也可以将GCM设备设置为主键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.