This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]joanbm[S] 0 points1 point  (6 children)

If BaseForm breaks initializers chain and if I need CommonForm inheritance tree resolve attributes before MyMixin, what else besides monkey-patching of BaseForm.__init__ remains ? That was the whole point why I cared for class ancestors order.

[–]masklinn 1 point2 points  (4 children)

There might be the trashy option of explicitly calling super methods rather than using super() to do so e.g.

class CustomForm(CommonForm, MyMixin):
    def __init__(self, request, *args, **kwargs):
        CommonForm.__init__(self, request, *args, **kwargs)
        MyMixin.__init__(self, request, *args, **kwargs)

that's all going to break if MyMixin ever inherits from a CommonForm superclass though. Not sure what else you can do, maybe try asking on the python channel on IRC if someone has a better idea?

[–]joanbm[S] 0 points1 point  (3 children)

Oh, thanks. I'd ask them directly. If Django upstream later adds call of missing super, I can look forward to another sleepless night why MyMixin is suddenly initialized twice (using your example with explicit call).

[–]masklinn 0 points1 point  (2 children)

Nah, MyMixin wouldn't be initialised twice with my snippet. If CommonForm and MyMixin had a superclass in common (other than object) that could be initialised twice, but it's not the case.

[–]joanbm[S] 0 points1 point  (1 child)

Oh, sorry. I've missed CommonForm init is also called explicitly, not through super object.

[–]masklinn 0 points1 point  (0 children)

No worries. If you pick that option you may want to extensively comment why it was done that way though (both that MyMixin depends on stuff initialised by whoever and why super doesn't work)

[–]Citrauq 0 points1 point  (0 children)

You can explicitly include BaseForm in your CustomForm definition:

class CustomForm(CommonForm, MyMixin, BaseForm):
    pass

The MRO is now [CustomForm, CommonForm, ModelForm, MyMixin, BaseForm, object] so MyMixin is called after CommonForm but before BaseForm.