Duplicating Template Tag
This template tag will duplicate its contents according to a variable or integer supplied to it. {% duplicate 3 %}a{% endduplicate %} This would return: > aaa
- template-tag
- duplicate
- loop
This template tag will duplicate its contents according to a variable or integer supplied to it. {% duplicate 3 %}a{% endduplicate %} This would return: > aaa
This is useful to run before you add a unique key to a character field that has duplicates in it. It just adds numbers to the end of the contents, so they will be unique. It takes a model class and a field name. The model class can be a South fake orm object, so this can be used inside data migrations.
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.
3 snippets posted so far.