gameboy86/matchbox-orm

How can make my model can inherit?

Closed this issue · 2 comments

Hi,
in my project, I used similar structures with multiple classes,
such as,

class UserDatabase(models.Model):
    class Meta:
        collection_name = 'User'
    birth = models.TimeStampField()
    email = models.TextField()
    name = models.MapField()

    def __unicode__(self):
        return self.id

    def to_dict(self):
        return self.__dict__

class AnnotatorDatabase(models.Model):
    class Meta:
        collection_name = 'annotators'
    total_annotation = models.IntegerField()

    def __unicode__(self):
        return self.id

    def to_dict(self):
        return self.__dict__

So I want to make above codes to like below.

class BaseDatabase(models.Model):

    def __unicode__(self):
        return self.id

    def to_dict(self):
        return self.__dict__

class Userdatabase(BaseDatabase):
    birth = models.TimeStampField()
    email = models.TextField()
    name = models.MapField()

    class Meta:
        collection_name = 'User'

class AnnotatorDatabase(BaseDatabase):
    total_annotation = models.IntegerField()

    class Meta:
        collection_name = 'Annotator'

but it shows error, AttributeError: Can't inherit from non abstract class so coud you give me any tip for the better design?

Thank you.

Hi Paul-Kim, you must use abstract meta attribute (https://github.com/gameboy86/matchbox-orm#abstract-model) so:

class BaseDatabase(models.Model):

def __unicode__(self):
    return self.id

def to_dict(self):
    return self.__dict__

class Meta:
    abstract = True

btw i must change error message from "Can't inherit from non abstract class" to "Can't inherit from non abstract model"

I just found a Django tutorial about using metaclass information and resolve this issue with the exactly same way of yours. Now I understand this library is Django style. Thank you!