简体   繁体   中英

Django multiple model inheritance, django-push-notifications

I am writing a back end for a mobile app using Django 1.8. The django-push-notifications lib provides the GCMDevice model. The problem is I already have a Device model with some mandatory field and some logic I don't want to lose.

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). 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). Maybe OneToOne association will do the work?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM