[Answered ]-Django: Question about model architecture

2đź‘Ť

âś…

It’s not exactly clear what your “implementation” classes aim to achieve just by looking at the code above. It’s unclear whether the implementation classes are yet another ORM model, or just a custom class which provides a save() method.

A couple of gotchas just from skimming what’s above.

The line:

self.impl = MyImplementationClass(my_facade_class_instance=self, some_field=self.some_field, account=self.account)

comes before the call to super(), which means it is highly likely self.some_field and self.account are not properly initialized at the time you pass it to the other model.

The second gotcha comes in the fact that as written above, the two instances will (likely, depends on how MyImplementationClass is written) contain cyclic references to each other. This means that the reference count will not be 0 when the objects go out of scope. The cyclic GC will (probably) eventually garbage collect these objects, but you lose out on deterministic garbage collection, which (in my opinion) is a really powerful feature of Python.


It appears you are trying to implement what’s known as a “generic relation”, which is a feature already provided by django’s contrib.contenttypes app: http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#id1 If you just want an object which can refer to one of many model types, you can do so by using the generic relations from “contenttypes”.

👤Crast

Leave a comment