Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

Jibola
Copy link
Contributor

@Jibola Jibola commented Mar 11, 2025

First draft multiple models in a collection:

  • Tests
  • Documentation
  • Index Creation Support

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)
Copy link
Collaborator

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?

Copy link
Collaborator

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
Copy link
Collaborator

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]
Copy link
Collaborator

@WaVEV WaVEV Mar 20, 2025

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__
Copy link
Collaborator

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.

@timgraham
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants