Login

Tag "denormalize"

Snippet List

Duplicate related objects of model instance

I've got a bunch of `Models` that form a tree like structure. I'd like to duplicate them all changing one field to something else. Say for example I've got a `Website` which has `Pages` and `Links` and all kinds of other `Models`. Each one of these belong to a `User` (through a foreign key relation). I could use `duplicate` to create a copy of an entire website and give it to another `User` with something like this: class Website(Model): owner = ForeignKey('auth.User') ... class Link(Model): owner = ForeignKey('auth.User') ... class Page(Model): owner = ForeignKey('auth.User') ... ################################## website = Website.objects.get(pk=1) new_owner = User.objects.get(pk=1) duplicate(website, new_owner, 'owner') For a in depth example of the problem see: [Duplicating Model Instances @ STO](http://stackoverflow.com/questions/437166/duplicating-model-instances-and-their-related-objects-in-django-algorithm-for-r) *Note* * Not tested with anything but simple Foreign Key relations - the model ordering is _very_ naive.

  • copy
  • duplicate
  • denormalize
Read More

1 snippet posted so far.