I was wondering if I have a couple of models which both include fields like "meta_keywords" or "slug" which have to do with the web page the model instance will be displayed on, whether it would be advisable to break those page metadata elements out into their own class, say PageMeta, and have my other models subclass those via multiple inheritance?
Configuring Django to use remote mysql server?
Apache/Django freezing after a few requests
HTTPResponse - Return organised data (table?)
Nontrivial multiple inheritance in Python requires Advanced Techniques to deal with the metaclass/metatype conflict.
How can I make a Django query for the first occurrence of a foreign key in a column?
Look over this recipe from the ActiveState archives and see if it looks like the kind of stuff you like:.
django admin: company branches must manage only their records across many models
Extract from linked recipe:
The simplest case where a metatype conflict happens is the following.Consequently, I recommend limiting your use of multiple inheritance in Python to the following cases:.
Why is Standard Input is not displayed as I type in Mac OS X Terminal application?
Consider a class A with metaclass
M_Aand a class B with an independent metaclass
M_B; suppose we derive C from A and B.
Django popup box Error?? Running development web server
The question is: what is the metaclass of C ? Is it
Django: do I need to restart Apache when deploying?
The correct answer (see the book "Putting metaclasses to work" for a thoughtful discussion) is
M_Cis a metaclass that inherits from
M_B.. However, Python is not that magic, and it does not automatically create
Instead, it raises a TypeError, warning the programmer of the possible confusion..
- You must, because your problem domain requires you to combine two separately-maintained single-inheritance libraries.
- You have achieved such fluency with metatype and metaclass that you can write recipe 204197 or its equivalent as easily and confidently as you can write a print statement.
It is clear that indiscriminate use of multiple inheritance is a maintenance nightmare, given the reliance in Python on conventions to avoid accidental name conflicts..Here he is again in PEP 253, which describes the ideas which were incorporated into Python, but not the implementation:.
Metatypes determine various policies for types, such as what happens when a type is called, how dynamic types are (whether a type's dict can be modified after it is created), what the method resolution order is, how instance attributes are looked up, and so on.. I'll argue that left-to-right depth-first is not the best solution when you want to get the most use from multiple inheritance.. I'll argue that with multiple inheritance, the metatype of the subtype must be a descendant of the metatypes of all base types..This does not mean you shouldn't use multiple inheritance; I'm just warning you so you won't be suprised one day to find yourself slapping your forehead and exclaiming "D'oh! The metatype of one of my subtypes isn't a descendant of the metatypes of all its base types! Don't you hate when that happens?".