[Django]-Django – Model class interfaces?

3👍

I think this is more of a Python question. You can certainly look at Django abstract base classes but I recommend you do the more pythonic thing and not worry about trying to enforce these class interfaces. Either you implemented the method or you didn’t, let the calling code deal with it.

👤Henry

7👍

You can use the abstract = True aspect of the Meta class.

class BaseModelInterface(models.Model):
    class Meta:
        abstract = True

class ActualModel(BaseModelInterface):
    [...normal model code...]

Documentation: http://docs.djangoproject.com/en/1.3/ref/models/options/

With that said, generally duck typing is considered to be “the Python way”, and your calling code should test for method presence (if hasattr(instance, 'method_name')). That said, you know your particular implementation better than we do, so you can use abstract = True to get the behavior you want. 🙂

Leave a comment