Multiple inheritance (model-based) broken for __init__ of common fields in diamond inheritance
|Reported by:||mikemintz||Owned by:||Malcolm Tredinnick|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
I am using the latest version of Django SVN r10558.
My models are:
class Restaurant(models.Model): name = models.CharField(max_length=255) class Bar(Restaurant): min_age = models.IntegerField() class Pizzeria(Restaurant): specialty = models.CharField(max_length=255) class PizzeriaBar(Bar, Pizzeria): pizza_bar_specific_field = models.CharField(max_length=255)
This yields the following inheritance diagram:
Model | Restaurant / \ Bar Pizzeria \ / BarPizzeria
My problem is that setting the name field in PizzeriaBar.__init__ does not work.
>>> p = PizzeriaBar(name="Michaels", min_age=21, specialty="Cheese", pizza_bar_specific_field="Doodle") >>> print (p.name, p.min_age, p.specialty, p.pizza_bar_specific_field) ('', 21, 'Cheese', 'Doodle') >>> p = PizzeriaBar() >>> p.name = "Michaels" >>> p.min_age = 21 >>> p.specialty = "Cheese" >>> p.pizza_bar_specific_field = "Doodle" >>> print (p.name, p.min_age, p.specialty, p.pizza_bar_specific_field) ('Michaels', 21, 'Cheese', 'Doodle')
So this problem only comes up when you are using multiple inheritance, and you set a field defined in a shared superclass of the current model's superclasses (i.e., diamond inheritance). I know diamond inheritance isn't great to use, but it's useful in my application (in which most models inherit from a top-level Item model), and since it works when using setattr, it seems like it should be possible to work in __init__.
Change History (26)
comment:14 Changed 7 years ago by
|Owner:||changed from jianhuang to Malcolm Tredinnick|
|Status:||assigned → new|
|Triage Stage:||Ready for checkin → Accepted|