-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
INTPYTHON-487 Polymorphic Collection Support #269
base: main
Are you sure you want to change the base?
Conversation
def __iter__(self, *args, **kwargs): | ||
for obj in super().__iter__(*args, **kwargs): | ||
model_class = apps.get_model(obj._meta.app_label, obj._t) | ||
yield model_class.objects.get(pk=obj.pk) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 what is the flow here? is it doing N+1 queries to get the data?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jib mentioned that it's a temporary hack.
if issubclass(_base, MultiModel): | ||
MultiModel._subclasses.setdefault(_base, []).append(cls) | ||
|
||
# Get all the subclasses for my model |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my model? our model. (don't know if it is good to post a meme here)
node = stack.pop() | ||
stack.extend(cls._subclasses.get(node, [])) | ||
acc.add(node) | ||
return [obj.__name__ for obj in acc] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe adding obj.__module__
and then __name__
could potentially fix a collision. two modules could have the same name in two different files.
Lets say:
file1.py:
from base import A
class B(A):
pass
file2.py
from base import A
class B(A):
pass
base.py
class A:
pass
main.py
from file1 import B
from file2 import B as B2
print(B.__name__, B2.__name__) # it prints-> B, B
print(B.__module__, B2.__module__). # it prints -> file2 file3
# Save the classname as the _t before saving | ||
def save(self, *args, **kwargs): | ||
if not self._t: | ||
self._t = self.__class__.__name__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the above comment is ok, we should also modify this one.
It's an interesting implementation, but without support for formsets and admin, this seems of limited value. You'd have to edit all the child models separately since the add/edit forms for the parent model don't include child fields. Also, the fact that migrations only manage the parent model creates problems with adding/removing/renaming/changing fields on the child models (and of course, indexes, as you already mentioned). I think it would be better to target this idea for post-GA (e.g. Django 6.0) when we can have an implementation that relies on patches to Django that would make this work more smoothly. |
First draft multiple models in a collection: