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

Nested ClusterableModel relations are not copied #153

Open
vbudovski-alliance opened this issue Jan 11, 2022 · 1 comment
Open

Nested ClusterableModel relations are not copied #153

vbudovski-alliance opened this issue Jan 11, 2022 · 1 comment

Comments

@vbudovski-alliance
Copy link

Consider nested relations in the context of Band, Album, and Song models.

beatles = Band(name="The Beatles", albums=[
    Album(name="Please Please Me", songs=[
        Song(name="I Saw Her Standing There"),
        Song(name="Love Me Do"),
    ]),
])
beatles.save()

beatles_clone = Band("The Beatles 2020 comeback")
beatles.copy_all_child_relations(beatles_clone, commit=True)

Cloning the beatles object will not clone the songs attached to the album.

@vbudovski-alliance
Copy link
Author

I was able to copy the nested records using:

        for child_object in source_manager.all().order_by("pk"):
            old_pk = child_object.pk
            # TODO: What do we do about the child_object_map of the child_object?
            if isinstance(child_object, ClusterableModel):
                child_object, _ = child_object.copy_cluster()

            child_object.pk = None
            setattr(child_object, parental_key_name, target.id)
            target_manager.add(child_object)

Not sure if this is the right solution or what to do about the nested child_object_map situation.

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

No branches or pull requests

1 participant